I’m currently working on an iOS application where you can take an image and upload it to the server.
I have this working fine in the simulators however when testing on the device I get a received memory warning and then it crashes.
There is quite a bit more in my code I’ll just try to show the bits that are relevant to my problem:
I’m trying to send two images taken using the photo picker on the device this is saved to the phone and passed through to a payment screen:
- (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];
}
On the payment screen I have this code to send it to the server and the php script sends back the paypal service:
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];
Sorry if this isn’t too clear please let me know. It sends the images to the server but the app will crash in the process. I have tried resizing the images however i need them at the best quality possible. If I send scaled down images using:
UIGraphicsBeginImageContext( frontSize );
[frontimg drawInRect:CGRectMake(0,0,242,444)];
UIImage* realfrontImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *frontImageData = UIImageJPEGRepresentation(image, 1.0);
It works however the image quality is 72PPI when I need it up around 300.
Any help would be much appreciated.
It’s an unusual use of the webview to do a post. It’s possible that the webview (or something else essential) is getting released on memory warning. Can you replace it with this and see if it helps?