我目前正在开发一个 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。
任何帮助将非常感激。