atrik
atrik

Reputation: 659

Uploading image to server takes forever

I am trying to upload a picture form the iphone, from camera roll or a new one, I am using this code for getting the image in the - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

self.image = UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerEditedImage]);

and I send the image to the server using this

-(void)subeFoto1{

NSString *urlString = [NSString stringWithFormat:@"http://bonsai.com.ec/scrum/SubeImagen.php"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"img.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:self.image]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

self.urlImage = [NSString stringWithFormat:@"http://bonsai.com.ec/scrum/%@", returnString];
NSLog(@"URL %@", self.urlImage);

}

but it is taking FOREVER!! I changed the php so now the images that I save are 250kb but the problem is in the app I think, any thoughts ?

Upvotes: 0

Views: 405

Answers (1)

jsd
jsd

Reputation: 7703

A few problems here:

  1. You're sending the raw image data. You should convert it to jpeg first, this will make it MUCH smaller and thus upload faster. Use UIImageJPEGRepresentation to convert. You can specify the quality. Lower quality = smaller image.
  2. Don't ever use sendSynchronousRequest in an iOS app. This blocks the thread and can lead to your app being terminated. You will need to implement a NSURLConnectionDelegate and all the asynchronous methods. It's a pain in the behind to do it yourself, honestly. I would use a wrapper class like MKNetworkKit.

Here's an example of how you might upload the image with MKNetworkKit:

NSData *imageData = UIImageJPEGRepresentation(self.image, 0.85f);
MKNetworkOperation *op = [[MKNetworkOperation alloc] initWithURLString:@"http://bonsai.com.ec/scrum/SubeImagen.php"
                                                                params:nil
                                                            httpMethod:@"POST"];
[op addData:imageData forKey:@"uploadedfile" mimeType:@"image/jpeg" fileName:@"img.jpg"];

[op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
    // this will be called when the request succeeds
} errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
    // this will be called if the request fails
}];
[[NSOperationQueue mainQueue] addOperation:op];

One thing to note is that since this is an asychronous operation, your program will not stop! The upload will happen behind the scenes. The completion handler or error handler will be called at some point in the future when the operation finishes.

Upvotes: 1

Related Questions