iOS - UIImagePickerController在设备上崩溃

sol*_*eil 3 crash uiimagepickercontroller ios

此代码在模拟器中运行良好,但每次在设备(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];
}
Run Code Online (Sandbox Code Playgroud)

}

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

}
Run Code Online (Sandbox Code Playgroud)

Jan*_*ann 5

这是否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];