使用UIImagePickerController时的内存警告

时间:2013-11-21 11:36:42

标签: ios memory uiimagepickercontroller memory-warning

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

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

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

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

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

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

我的代码:

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

2 个答案:

答案 0 :(得分:6)

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

self.image = [info objectForKey:UIImagePickerControllerOriginalImage];

我把它改为:

UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];

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

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

答案 1 :(得分:2)

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    [[NSURLCache sharedURLCache] removeAllCachedResponses];

}

清除我正在使用“UIImagePickerController”的类中的缓存,为我工作!!!