从iOS上传图像到PHP Web服务器

我正在开发一个iOS应用程序,将上传一个图像(从前置摄像头或画廊)到networking服务器。 我不知道如何从iOS发送图像,以及如何在PHP中检索。 请帮助。

你可以使用AFNetworking框架( https://github.com/AFNetworking/AFNetworking ),它会为你节省很多时间,并且有一些例子如何上传图像和处理响应。

这里是一个从上面提到的源file upload的例子:

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