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.
Asked
Active
Viewed 5,362 times
1
-
you can save image in database table after converting to `NSData` in BLOB field on mySql – Nirav Gadhiya Jun 25 '13 at 10:14
-
@NiravGadhiya Yeah I have same thoughts but I couldn't find useful source code. – Ali Shahid Jun 25 '13 at 10:16
-
1I posted a solution to this a while back. http://stackoverflow.com/questions/1266176/upload-file-to-ftp-server-on-iphone/1272889#1272889 – Dutchie432 Jun 25 '13 at 10:17
1 Answers
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