1

I am developing an iOS app that will upload an image(taken from front camera or from gallery) to web server. I have no idea how to send that image from iOS and how to retrieve it in PHP. Kindly help.

Ali Shahid
  • 516
  • 1
  • 5
  • 21

1 Answers1

7

You can use AFNetworking framework (https://github.com/AFNetworking/AFNetworking), it will save a lot of time for you and there are some examples how to upload images and handle responses.

Here is an example of file upload from the source mentioned above:

NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[httpClient enqueueHTTPRequestOperation:operation];
haynar
  • 5,961
  • 7
  • 33
  • 53