XCode将图像发送到服务器

时间:2012-04-25 18:41:34

标签: iphone objective-c ios image

我目前正在开发一个iOS应用程序,您可以在其中拍摄图像并将其上传到服务器。

我在模拟器中工作正常但是在设备上测试时我收到了内存警告,然后崩溃了。

我的代码中还有更多内容我会尝试显示与我的问题相关的位:

我正在尝试使用设备上的照片选择器发送两张图像,然后将其保存到手机并传递到付款界面:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    {
        UIImage *image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString: "name.jpg"] ];
        NSData* data = UIImageJPEGRepresentation(image,1.0);
        [data writeToFile:path atomically:YES];
    }

在付款界面我将此代码发送到服务器,php脚本发回PayPal服务:

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithString: @"name.jpg"] ];

    NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:path]);


    NSString *urlString = @"http://server.com/payment.php?";
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = [NSString stringWithString:@"---------------------------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 stringWithString:@"Content-Disposition: form-data; name=\"backfile\"; filename=\"back.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [webView loadRequest:request];


    [self.view addSubview:webView];
    [request setHTTPBody:body];

对不起,如果不太清楚请告诉我。它将图像发送到服务器,但应用程序将在此过程中崩溃。我已经尝试调整图像大小但是我需要它们以尽可能最好的质量。如果我使用以下方式发送缩小的图像:

    UIGraphicsBeginImageContext( frontSize );
    [frontimg drawInRect:CGRectMake(0,0,242,444)];
    UIImage* realfrontImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *frontImageData = UIImageJPEGRepresentation(image, 1.0);

然而,当我需要大约300时,它的图像质量是72PPI。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

使用webview进行发布是不寻常的。 webview(或其他必要的东西)可能会因内存警告而被释放。你可以用它替换它,看它是否有帮助?

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
    NSLog(@"got response %d, data = %@, error = %@", [httpResponse statusCode], data, error);
}];

答案 1 :(得分:0)

向所有UIViewControllers的didReceiveMemoryWarning方法和viewWillUnload方法添加NSLog调试消息,以查看您的UIViewControllers是否正在释放您所依赖的对象。