将图像上传到服务器需要永远

时间:2014-03-20 19:54:45

标签: ios iphone objective-c uiimagepickercontroller

我正在尝试上传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,但问题是在我认为的应用程序,任何想法?

1 个答案:

答案 0 :(得分:1)

这里有一些问题:

  1. 您正在发送原始图像数据。你应该首先将它转换为jpeg,这将使它变得更小,从而更快上传。使用UIImageJPEGRepresentation进行转换。您可以指定质量。质量较差=图像较小。
  2. 不要在iOS应用中使用sendSynchronousRequest。这会阻止线程,并可能导致您的应用程序被终止。您需要实现NSURLConnectionDelegate和所有异步方法。老实说,自己做这件事背后是痛苦的。我会使用像MKNetworkKit这样的包装类。
  3. 以下是如何使用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];
    

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