4

Im getting a memory warning when Im using the camera on an iPhone. Im also using ARC.

When you take a photo and press the 'use Photo' button on the camera view controller I get a memory warning. The intention is once the 'use Photo' button is pressed that it changes the contents of the an ImageView.

I thought the memory issue might be due to the fact that the image that is captured is full screen, and the ImageView is 250h 250w. But I tried scaling down the size of the image taken by the camera and then assign it to the ImageView. However this still did not work, even when I resized it to 100 x 100.

Secondly, I then did not assign the photo taken by the camera to the ImageView but it still has the memory warning.

I looked at other answers here and attempted the two above but it is still there. I will show my code below. Will this affect my submission to the app store? Surely if it is such a common occurence that it is a bug or there is a work around? It would be great if one could look at the code provided and spot the error or suggest how to handle this memory warning?

My app is 95+% finished apart from this memory warning so it is getting close to submission time.

My code:

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

2 回答 2

6

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

self.image = [info objectForKey:UIImagePickerControllerOriginalImage];

我将其更改为:

UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];

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

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

于 2013-12-19T14:41:41.480 回答
2
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    [[NSURLCache sharedURLCache] removeAllCachedResponses];

}

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

于 2014-09-23T10:29:49.657 回答