使用UIImagePickerController时的内存警告 [英] Memory warning when using UIImagePickerController

查看:116
本文介绍了使用UIImagePickerController时的内存警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPhone上使用相机时会收到内存警告。我也在使用ARC。

Im getting a memory warning when Im using the camera on an iPhone. Im also using ARC.

当您拍照并按下相机视图控制器上的使用照片按钮时,我会收到内存警告。目的是按下使用照片按钮,它会改变ImageView的内容。

When you take a photo and press the 'use Photo' button on the camera view controller I get a memory warning. The intention is once the 'use Photo' button is pressed that it changes the contents of the an ImageView.

我认为内存问题可能是由于捕获的图像为全屏,ImageView为250h 250w。但我尝试缩小相机拍摄的图像的大小,然后将其分配给ImageView。然而,即使我将其调整为100 x 100,这仍然无效。

I thought the memory issue might be due to the fact that the image that is captured is full screen, and the ImageView is 250h 250w. But I tried scaling down the size of the image taken by the camera and then assign it to the ImageView. However this still did not work, even when I resized it to 100 x 100.

其次,我没有将相机拍摄的照片分配给ImageView,但它还有内存警告。

Secondly, I then did not assign the photo taken by the camera to the ImageView but it still has the memory warning.

我在这里查看了其他答案并尝试了上述两个但它仍然存在。我将在下面显示我的代码。这会影响我对应用商店的提交吗?当然,如果这是一个常见的事件,它是一个错误或有一个解决方案?如果可以查看提供的代码并发现错误或建议如何处理此内存警告会很好吗?

I looked at other answers here and attempted the two above but it is still there. I will show my code below. Will this affect my submission to the app store? Surely if it is such a common occurence that it is a bug or there is a work around? It would be great if one could look at the code provided and spot the error or suggest how to handle this memory warning?

我的应用程序已完成95 +%内存警告,因此它接近提交时间。

My app is 95+% finished apart from this memory warning so it is getting close to submission time.

我的代码:

- (IBAction)takePhoto:(id)sender {

self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing=NO;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
     [self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentViewController:self.imagePicker animated:YES completion:NULL];

}
else{
    [self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:self.imagePicker animated:YES completion:NULL];
 }



}

- (IBAction)choosePhoto:(id)sender {
self.imagePicker2 = [[UIImagePickerController alloc] init];
self.imagePicker2.delegate = self;
self.imagePicker2.allowsEditing=NO;
[self.imagePicker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.imagePicker2 animated:YES completion:NULL];
}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
CGRect rect = CGRectMake(0,0,100,100);

UIGraphicsBeginImageContext( rect.size );
[self.image drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.snapImage setImage:picture1];

[self.uploadImageBtn setHidden:NO];
[self dismissViewControllerAnimated:YES completion:NULL];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

[self dismissViewControllerAnimated:YES completion:NULL];
}


推荐答案

我没有找到一个好的解决方案但我不会将原始图像存储在属性中,因为原始图像占用大约30MB的内存。所以代替:

I didnt find a good solution but I would not store the raw image in a property because the raw image takes up roughly 30MB of memory. So instead of:

self.image = [info objectForKey:UIImagePickerControllerOriginalImage];

我将其更改为:

UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];

这样,图像在不再使用时会被销毁。注意:我在iPhone 4系列和5上测试了这种新方法。内存警告只出现在4系列而不是5系列上。

This way the image is destroyed when it is no longer in use. Note: I've test this new method on iPhone 4 series and 5. The memory warning only appears on the 4 series not the 5.

从网上看看已经向Apple提交了许多有关Camera和iOS7的错误报告。例如,当您启动相机时会不定期地进行黑色预览 - 这与iOS7相关联,而iPhone 4系列则不是5。这可能是处理器功率的差异 - 但我不确定。我的应用程序获得了应用程序商店的批准,因此内存警告不会成为问题 -

From looking around the web there have been many bug reports submitted to Apple in regards to the Camera and iOS7. For instance, irregularly when you launch the Camera it will give a black preview - this is linked to iOS7, and more so the iPhone 4 series not 5. This is probably the difference in the processor power - but I am not sure. My app got approved for the app store so the memory warning will not be an issue –

这篇关于使用UIImagePickerController时的内存警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆