【问题标题】:Upload image to PHP web server from iOS [duplicate]从 iOS 将图像上传到 PHP Web 服务器 [重复]
【发布时间】:2013-06-22 02:06:01
【问题描述】:

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

【问题讨论】:

  • 在mySql的BLOB字段中转换为NSData后可以将图片保存到数据库表中
  • @NiravGadhiya 是的,我也有同样的想法,但我找不到有用的源代码。
  • 我不久前发布了一个解决方案。 stackoverflow.com/questions/1266176/…

标签: php ios json image web-services


【解决方案1】:

您可以使用 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];

【讨论】:

  • 您的回答很有帮助,但我不想使用 AFNetworking。
  • 你能编辑你对 AFNetworking 2.0 的答案吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 2015-03-29
  • 2011-10-15
相关资源
最近更新 更多