【问题标题】:Memory warning when using UIImagePickerController使用 UIImagePickerController 时的内存警告
【发布时间】:2013-12-05 20:39:50
【问题描述】:

我在 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];
}

【问题讨论】:

  • 你找到这个问题的解决方案了吗,自从我更新到iOS 7后,我仍然面临这个问题。
  • 不,我没有找到好的解决方案,但这有帮助:“我不会将原始图像存储在属性中,因为原始图像占用大约 30MB 的内存。”所以我把self.image = [info objectForKey:UIImagePickerControllerOriginalImage];改为UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];,而不是self.image = [info objectForKey:UIImagePickerControllerOriginalImage];。这样,图像在不再使用时就会被破坏。注意:我在 iPhone 4 系列和 5 上测试过这种新方法。内存警告只出现在 4 系列而不是 5。
  • 从网上看,有许多关于相机和 iOS7 的错误报告提交给 Apple。例如,当您启动相机时,它会不定期地显示黑色预览 - 这与 iOS7 相关联,更多的是 iPhone 4 系列而不是 5。这可能是处理器能力的差异 - 但我不确定。我的应用已获得应用商店的批准,因此内存警告不会成为问题
  • 感谢您的回复,我也收到了 iPhone 4 系列的警告。我的应用在 iPad 上运行良好。
  • 没问题很乐意提供帮助 ;) 请记住,iOS7 不是用 iPhone 4 构建的,它是随 iPhone 5 发布的。总会有性能问题

标签: ios memory uiimagepickercontroller memory-warning


【解决方案1】:
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    [[NSURLCache sharedURLCache] removeAllCachedResponses];

}

在我使用“UIImagePickerController”的类中清除缓存,对我有用!!!

【讨论】:

    【解决方案2】:

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

    self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    

    我改成:

    UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
    

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

    通过浏览网络,有许多关于相机和 iOS7 的错误报告提交给 Apple。例如,当您启动相机时,它会不定期地显示黑色预览 - 这与 iOS7 相关联,更多的是 iPhone 4 系列而不是 5。这可能是处理器能力的差异 - 但我不确定。我的应用已获得应用商店的批准,因此内存警告不会成为问题 -

    【讨论】:

      猜你喜欢
      • 2011-09-02
      • 1970-01-01
      • 2013-10-04
      • 2011-03-07
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      相关资源
      最近更新 更多