¿Necesita ayuda para cargar múltiples imágenes grandes desde el dispositivo ios?

Busqué con éxito todas las imágenes url en mi galería de imágenes de iPhone usando la biblioteca alasset y las almacené en una matriz. Ahora estoy intentando upload al server, aquí está mi código:

Intenté dos enfoques, pero ambos se bloquean después de iterar alnetworkingedor de 10 imágenes, sin ningún logging de locking. Las imágenes no se cargan en el server, se bloquea antes de cargarlas.

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: usando 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:@"http://sofes.miximages.commypage.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)shanetworkingClient; @end @implementation MyHTTPClient + (id)shanetworkingClient { static MyHTTPClient *shanetworkingClient; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ shanetworkingClient = [[MyHTTPClient alloc] initWithBaseURL:nil]; }); return shanetworkingClient; } @end @implementation MyViewController - (void)uploadImages { NSURLRequest *myRequest; __block UIImage *img; __block NSData *imgData; __block NSString *fName; myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"http://iosberry.commypage.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 shanetworkingClient] enqueueHTTPRequestOperation:operation] } @end 

Debería [operation start]; después de configurar la finalización y el progreso de las devoluciones de llamada.

Sus lockings son potencialmente debido a la sobrecarga de la memory. En primer lugar, en la sección 1 , debe drenar la agrupación de autorelease en cada iteración, como tal:

 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]); }]; } } 

Los methods como imageWithCGImage: y UIImageJPEGRepresentation devuelven objects grandes autoelevados, por lo que debe asegurarse de que se lanzan lo antes posible para liberar memory.

En la seccion 2:

En lo que respecta a AFNetworking, llamar [operation start] es inútil. La operación se lanzará tan pronto como esté fuera del scope, por lo que es poco probable que se complete. Debe conservar la instancia de AFHTTPClient (normalmente se realiza como un singleton, pero una propiedad es lo suficientemente buena) y enqueuer las operaciones llamando a:

 [httpClient enqueueHTTPRequestOperation:operation]