我成功地使用alasset库在我的
iphone图像库中获取了所有图像URL并存储在一个数组中.现在我想上传到服务器,这是我的代码:
我尝试了两种方法,但是在迭代了大约10张图像之后都崩溃了,没有任何崩溃日志.图像不会上传到服务器,它会在上传之前崩溃.
1:
NSData *imgData; UIImage *img; NSInputStream *stream;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://my.url.com"]];
for(int i=0; i<_dataContainer.count; i++)
{
img = [UIImage imageWithCGImage:[[[_dataContainer objectAtIndex:i] defaultRepresentation]fullResolutionImage]];
imgData = UIImageJPEGRepresentation(img, 1.0);
stream = [[NSInputStream alloc] initWithData:imgData];
[request setHTTPBodyStream:stream];
[request setHTTPMethod:@"POST"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]);
}];
}
2:使用Afnetworking
AFHTTPClient *client= [[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:@"http://my.url.com"]];
NSURLRequest *myRequest;
__block UIImage *img;
__block NSData *imgData;
__block NSString *fName;
myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"/images/mypage.php" parameters:nil constructingBodyWithBlock:
^(id <AFMultipartFormData>formData)
{
img = [UIImage imageWithCGImage:[[[_dataContainer objectAtIndex:0] defaultRepresentation]fullResolutionImage]];
imgData = UIImageJPEGRepresentation(img, 1.0);
fName = [self returnDateTimeWithMilliSeconds];
[formData appendPartWithFileData:imgData name:@"photo" fileName:[NSString stringWithFormat:@"%@.jpg",fName] mimeType:@"image/jpeg"];
NSLog(@"FN=>%@ | Size=>%@",fName, [NSByteCountFormatter stringFromByteCount:[imgData length] countStyle:NSByteCountFormatterCountStyleFile]);
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:myRequest];
[operation start];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"Success Data -> %@", operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed");
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Progrees -> %f", ((float)((int)totalBytesWritten) / (float)((int)totalBytesExpectedToWrite)));
}];
最佳答案
@interface MyHTTPClient : AFHTTPClient
+ (id)sharedClient;
@end
@implementation MyHTTPClient
+ (id)sharedClient
{
static MyHTTPClient *sharedClient;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedClient = [[MyHTTPClient alloc] initWithBaseURL:nil];
});
return sharedClient;
}
@end
@implementation MyViewController
- (void)uploadImages
{
NSURLRequest *myRequest;
__block UIImage *img;
__block NSData *imgData;
__block NSString *fName;
myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"/images/mypage.php" parameters:nil constructingBodyWithBlock:
^(id <AFMultipartFormData>formData)
{
img = [UIImage imageWithCGImage:[[[_dataContainer objectAtIndex:0] defaultRepresentation]fullResolutionImage]];
imgData = UIImageJPEGRepresentation(img, 1.0);
fName = [self returnDateTimeWithMilliSeconds];
[formData appendPartWithFileData:imgData name:@"photo" fileName:[NSString stringWithFormat:@"%@.jpg",fName] mimeType:@"image/jpeg"];
NSLog(@"FN=>%@ | Size=>%@",fName, [NSByteCountFormatter stringFromByteCount:[imgData length] countStyle:NSByteCountFormatterCountStyleFile]);
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:myRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"Success Data -> %@", operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed");
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Progrees -> %f", ((float)((int)totalBytesWritten) / (float)((int)totalBytesExpectedToWrite)));
}];
[[MyHTTPClient sharedClient] enqueueHTTPRequestOperation:operation]
}
@end