使用UIImagePickerController时的内存警告

Dev*_*evC 4 memory uiimagepickercontroller ios 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];
}
Run Code Online (Sandbox Code Playgroud)

Dev*_*evC 6

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

self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
Run Code Online (Sandbox Code Playgroud)

我改成了:

UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
Run Code Online (Sandbox Code Playgroud)

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

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