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

Ali*_*hid 1 php json web-services image ios

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

hay*_*nar 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];
Run Code Online (Sandbox Code Playgroud)