簡體   English   中英

使用UIImagePickerController時的內存警告

[英]Memory warning when using UIImagePickerController

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

我沒有找到一個好的解決方案,但我不會將原始圖像存儲在屬性中,因為原始圖像占用大約30MB的內存。 所以代替:

self.image = [info objectForKey:UIImagePickerControllerOriginalImage];

我改成了:

UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];

這樣,圖像在不再使用時會被破壞。 注意:我在iPhone 4系列和5上測試了這種新方法。內存警告只出現在4系列而不是5系列上。

通過環顧網絡,已經有很多關於Camera和iOS7的Apple報告提交給了Apple。 例如,當您啟動相機時會不定期地進行黑色預覽 - 這與iOS7相關聯,而iPhone 4系列則不是5。這可能是處理器功率的差異 - 但我不確定。 我的應用程序獲得了應用程序商店的批准,因此內存警告不會成為問題 -

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

}

清除我正在使用“UIImagePickerController”的類中的Cache,為我工作!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM