从iOS上传图像到PHP Web服务器 [英] Upload image to PHP web server from iOS

查看:126
本文介绍了从iOS上传图像到PHP Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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

这篇关于从iOS上传图像到PHP Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆