Reputation: 659
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
Reputation: 7703
A few problems here:
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