Need help downloading a few large images from your ios device

I have successfully selected all the image URLs in my iphone image gallery using the alasset library and stored in an array. Now I'm trying to upload to the server, here is my code:

I tried two approaches, but both came out after iterating around 10 images without any kind of crash log. Images are not uploaded to the server; it crashes before uploading.

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: Using 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))); }]; 
+4
source share
3 answers
 @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 
+2
source

You must [operation start]; after setting callbacks complete and complete.

0
source

Your crashes are potentially related to memory overload. First, in section 1, you need to merge the autostart pool at each iteration, as such:

 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++) { @autoreleasepool { 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]); }]; } } 

Methods such as imageWithCGImage: and UIImageJPEGRepresentation return large objects with auto-implementation, so you need to ensure that they are freed by ASAP to free up memory.

In section 2:

As for AFNetworking, calling [operation start] useless. The operation will be released as soon as it goes out of scope, so it is unlikely that it will really end. You need to save the AFHTTPClient instance (usually executed as a singleton, but the property is good enough) and execute the queue operations on it, calling:

 [httpClient enqueueHTTPRequestOperation:operation] 
0
source

All Articles