2012-09-02 76 views
3

此代碼在模擬器中運行良好,但每次在設備(iPhone 3GS)上都會崩潰,正好當我拍照時。這段代碼有什麼問題嗎?當我使用分配進行配置時,活動內存在崩潰時只有3-4 MB,因此似乎應用程序內存不足。我正在使用ARC。iOS - UIImagePickerController在設備上崩潰

-(IBAction)chooseImageNew:(UIButton*)sender 
{ 
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
{ 

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
    imagePicker.delegate = self; 

    imagePicker.allowsEditing = YES; 
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 

    [self presentModalViewController:imagePicker animated:YES]; 
} 
else { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No Camera Available." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
UIImage *img = [info objectForKey:@"UIImagePickerControllerEditedImage"]; 
self.userPicture.image = img; 
[self.images replaceObjectAtIndex:0 withObject:img]; 

[self dismissModalViewControllerAnimated:YES]; 

} 
+0

annnnnd,是否有一個崩潰日誌生成時崩潰的設備? –

+0

在設備日誌下,有這樣的:我的應用程序名稱<65a5da31738337b18bc1a9e2dcaaaaaa> 23371(拋棄)(活動) – soleil

+0

從Xcode運行應用程序併發布控制檯和堆棧跟蹤中顯示的任何內容。 –

回答

5

這是否self.userPicture.image = img; 指定圖像到UIImageView的?

在你這樣做之前,你必須調整它的大小,ImagePickerController回調會給你一個JPEG表示形式的圖像,但只要你在UIImageView中顯示該圖像,數據就會被解碼爲原始格式。 3GS拍攝的照片分辨率爲2048x1536,這意味着12MB的數據,這對於3GS來說可能已經太多了。

有一些類別調整可用的,像這樣優秀的一個: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

如果您使用此分配給ImageView的前剛剛把這個:

UIImage* pickedImage = [sourceImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(960, 960) interpolationQuality:kCGInterpolationHigh];

+0

這是一個很好的建議。我實現了它,它不會崩潰。但是,我仍然收到「收到內存警告」。並且圖像不會顯示在UIImageView(self.userPicture)中。我們如何使用UIImagePickerController如果它使用了這麼多的內存? – soleil

+0

未顯示的原因是內存警告導致整個視圖重新加載。顯然這是一個很常見的問題,但即使我可以找到許多關於此的線索,但沒有任何好的答案。使用這麼多內存來處理相機的建議方法是什麼? – soleil

+0

謝謝。它的工作 –

2

您參考選擇器在你的方法返回時被釋放。將此伊娃:

的UIImagePickerController * imagePicker

編輯:另外,不釋放(ARC:無出)這個伊娃直到最後委託消息後:那就是,派遣塊主線程就這樣完成了一個運行循環之後! [問我怎麼知道這個:-)]

相關問題