内存警告使用UIImagePickerController时

当我在iPhone上使用相机时,我得到一个内存警告。 我也使用ARC。

当您拍摄照片并按下相机视图控制器上的“使用照片”button时,我会收到内存警告。 意图是一旦“使用照片”button被按下,它改变了一个ImageView的内容。

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

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

我看了这里的其他答案,并试图上述两个,但它仍然在那里。 我将在下面显示我的代码。 这会影响我提交给应用程序商店吗? 当然,如果这是一个常见的问题,这是一个错误或有工作? 如果能够查看提供的代码并发现错误或者build议如何处理这种内存警告,那将会很棒。

我的应用程序是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]; } 

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

 self.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

我把它改成:

 UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

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

从网上浏览,有关苹果相机和iOS7的许多bug报告。 例如,不定期地当你启动相机,它会给一个黑色的预览 – 这是连接到iOS7,而更多,所以iPhone 4系列不是5.这可能是在处理器的权力差异 – 但我不知道。 我的应用得到了app store的批准,所以内存警告不会是一个问题 –

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

清除类中的caching我使用“UIImagePickerController”,为我工作!