Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImagePickerControllerSourceTypeCamera hogging up memory

Tags:

iphone

I am using the UIImagePickerController in order to let the user select an image in my app by either taking a new pic or selecting an image from the gallery. Using gallery, the app works fine. But if I use the camera as a source, the app uses up a lot of memory and eventually gets killed after becoming terribly slow.

Can someone please tell me the optimum way to use UIImagePickerControllerSourceTypeCamera.

This is the code I am using

if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    return;
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[[self navigationController] presentModalViewController:picker animated:YES];
[picker release];
like image 394
lostInTransit Avatar asked Feb 19 '09 15:02

lostInTransit


2 Answers

There's no trick to using UIImagePickerController-- it uses a bunch of memory and you just have to live with that. Releasing it when you're done with it is as efficient as it gets.

That's with regard to the image picker itself, though. The other part of the question is what you're doing with the UIImage objects it returns to you. Those are big objects, by iPhone standards, and you really can't afford to keep many of them in memory. If you're displaying an image, that's life, but images that are not on-screen can be safely unloaded to a file via UIImageJPEGRepresentation() and NSData's writeToFile:atomically:.

If you do need to display several images, consider scaling them down. The camera's 1600x1200 is already much bigger than the screen, and with multiple on-screen images it's even more excessive. Scaling to lower resolutions reduces the memory requirements dramatically. Sample code for doing this is not hard to find-- see UIImagePickerController camera preview is portrait in landscape app for example.

like image 57
Tom Harrington Avatar answered Nov 08 '22 05:11

Tom Harrington


The UIImagePickerController leaks memory, as noted hereand after 7 or 8 uses causes your app to crash. You need to create a singleton UIImagePickerController for the life of your application to avoid this Apple defect.

like image 36
Jane Sales Avatar answered Nov 08 '22 04:11

Jane Sales