【问题标题】:Uploading image to server takes forever将图像上传到服务器需要很长时间
【发布时间】:2014-04-27 22:22:29
【问题描述】:

我正在尝试从 iphone 上传一张图片,从相机胶卷或新照片,我正在使用此代码在 - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *) 中获取图像信息

self.image = UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerEditedImage]);

我使用这个将图像发送到服务器

-(void)subeFoto1{

NSString *urlString = [NSString stringWithFormat:@"http://bonsai.com.ec/scrum/SubeImagen.php"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"img.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:self.image]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

self.urlImage = [NSString stringWithFormat:@"http://bonsai.com.ec/scrum/%@", returnString];
NSLog(@"URL %@", self.urlImage);

}

但它需要永远!我更改了 php,所以现在我保存的图像是 250kb,但问题出在我认为的应用程序中,有什么想法吗?

【问题讨论】:

  • 还显示 php 代码.. 可能有帮助...
  • 您应该改用异步请求。当前的实现将使您的应用程序感觉被锁定。另外你为什么要声明 returnData 直接指向 NSURLConnection?
  • 我需要获取最近上传的图片的url,所以我可以将它保存到数据库中......

标签: ios iphone objective-c uiimagepickercontroller


【解决方案1】:

这里有几个问题:

  1. 您正在发送原始图像数据。您应该先将其转换为 jpeg,这将使其更小,从而更快地上传。使用UIImageJPEGRepresentation 进行转换。您可以指定质量。较低的质量 = 较小的图像。
  2. 永远不要在 iOS 应用程序中使用 sendSynchronousRequest。这会阻塞线程并可能导致您的应用程序被终止。您将需要实现 NSURLConnectionDelegate 和所有异步方法。老实说,自己做这件事很痛苦。我会使用像 MKNetworkKit 这样的包装类。

这是一个使用 MKNetworkKit 上传图像的示例:

NSData *imageData = UIImageJPEGRepresentation(self.image, 0.85f);
MKNetworkOperation *op = [[MKNetworkOperation alloc] initWithURLString:@"http://bonsai.com.ec/scrum/SubeImagen.php"
                                                                params:nil
                                                            httpMethod:@"POST"];
[op addData:imageData forKey:@"uploadedfile" mimeType:@"image/jpeg" fileName:@"img.jpg"];

[op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
    // this will be called when the request succeeds
} errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
    // this will be called if the request fails
}];
[[NSOperationQueue mainQueue] addOperation:op];

需要注意的一点是,由于这是一个异步操作,您的程序不会停止!上传将在幕后进行。完成处理程序或错误处理程序将在操作完成后的某个时间点被调用。

【讨论】:

  • 你能提供一个更好的例子来说明如何上传图片吗?谢谢
  • 另一个很好的网络包装是 AFNetworking:github.com/AFNetworking/AFNetworking。 github页面上有一些很好的代码示例。
  • 编辑了我的答案以展示您将如何使用 MKNetworkKit。
猜你喜欢
  • 2014-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多