简体   繁体   中英

UIImagePickerControllerSourceTypeCamera hogging up memory

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];

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.

The UIImagePickerController leaks memory, as noted here and 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.

我知道这个答案是在事实之后,但我有同样类型的问题通过链接iPhone SDK 2.2和更高版本解决了它自己。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM