ios - 使用 UIImagePickerController 时的内存警告

标签 ios memory uiimagepickercontroller memory-warning

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

当您拍照并按下相机 View Controller 上的“使用照片”按钮时,我收到一条内存警告。目的是一旦按下“使用照片”按钮,它就会更改 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];
}

最佳答案

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

self.image = [info objectForKey:UIImagePickerControllerOriginalImage];

我把它改成:

UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];

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

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

关于ios - 使用 UIImagePickerController 时的内存警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20119918/

相关文章:

Python对低值整数在内存中的布局

c++ - 从缓冲区指针计算堆栈中的返回地址

c - 什么是连续内存块?

ios - UIImagePickerController 和 iCloud 照片

ios - 来自相机的 UIImagePickerController : Get back image with location data

ios - 从 iPhone 照片库中选择一个视频

ios - 激活在 Storyboard中卸载的约束

带有声音的 IOS 警报消息

ios - 将TabbarController中的一个ViewController替换为NavigationBar

ios - 将触摸从 UIButton 的 subview 传递回 UIButton