1

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

4

1 回答 1

7

您可以使用 AFNetworking 框架(https://github.com/AFNetworking/AFNetworking),它将为您节省大量时间,并且有一些如何上传图像和处理响应的示例。

以下是从上述来源上传文件的示例:

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];
于 2013-06-25T10:15:43.677 回答