简体   繁体   English

UIImagePickerControllerSourceTypeCamera占用内存

[英]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. 我正在使用UIImagePickerController,以便让用户通过拍摄新照片或从图库中选择图像来选择我的应用中的图像。 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. 有人可以告诉我使用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. 使用UIImagePickerController没有任何技巧 - 它使用了一堆内存,你只需要忍受它。 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. 问题的另一部分是你正在使用它返回给你的UIImage对象。 Those are big objects, by iPhone standards, and you really can't afford to keep many of them in memory. 根据iPhone的标准,这些都是大对象,而且你真的无法将其中的许多内容留在内存中。 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:. 如果您正在显示图像,那就是生命,但是可以通过UIImageJPEGRepresentation()和NSData的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. 相机的1600x1200已经比屏幕大得多,并且有多个屏幕上的图像,它甚至更加过分。 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. 执行此操作的示例代码并不难找到 - 例如,请参阅UIImagePickerController相机预览是横向应用程序中的纵向

The UIImagePickerController leaks memory, as noted here and after 7 or 8 uses causes your app to crash. 该泄漏的UIImagePickerController记忆,注意这里和后7层或8的用途使您的应用程序崩溃。 You need to create a singleton UIImagePickerController for the life of your application to avoid this Apple defect. 您需要在应用程序的生命周期中创建单独的UIImagePickerController以避免此Apple缺陷。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iPhone中使用UIImagePickerControllerSourceTypeCamera捕获图像10或11次后收到内存警告 - Receive memory warning after 10 or 11 times capturing image using UIImagePickerControllerSourceTypeCamera in iphone UIImagePickerControllerSourceTypeCamera 导航问题 - UIImagePickerControllerSourceTypeCamera problem with Navigation UIImagePickerControllerSourceSourceCamera在IPHONE模拟器中崩溃? - UIImagePickerControllerSourceTypeCamera crashes in IPHONE Simulator? UIImagePickerControllerSourceTypeCamera的自定义showCameraControls - Custom showsCameraControls for UIImagePickerControllerSourceTypeCamera 为 UIImagePickerControllerSourceTypeCamera 获取 SIGABRT - getting SIGABRT for UIImagePickerControllerSourceTypeCamera UIImagePickerControllerSourceTypeCamera被父视图对象覆盖 - UIImagePickerControllerSourceTypeCamera is covered by parent view objects 在UIImagePickerControllerSourceTypeCamera之后立即调整图像大小 - Immediately Resize Image after UIImagePickerControllerSourceTypeCamera 如何在横向而不是纵向中显示UIImagePickerControllerSourceTypeCamera - How to show UIImagePickerControllerSourceTypeCamera in Landscape instead of Portrait MPMoviePlayerController占用内存 - MPMoviePlayerController using up memory iPhone - 释放 memory - IPhone - free up memory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM