簡體   English   中英

iOS - UIImagePickerController在設備上崩潰

[英]iOS - UIImagePickerController crashes on device

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

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

}

這是self.userPicture.image = img; 將圖像分配給UIImageView?

在你這樣做之前你必須調整它,ImagePickerController回調為你提供JPEG表示的圖像,但是一旦你在UIImageView中顯示該圖像,數據就會被解碼為原始格式。 3GS采用2048x1536分辨率拍攝照片,轉換為12 MB數據,這對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];

當您的方法返回時,將釋放您對選擇器的引用。 把它變成伊娃:

UIImagePickerController * imagePicker

編輯:另外,不要釋放(ARC:nil out)這個ivar直到最后一個委托消息之后:也就是說,將一個塊發送到主線程,以便之后完成一個循環旋轉! [問我怎么知道這個:-)]

暫無
暫無

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

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