Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - UIImagePickerController crashes on device

This code runs fine in the simulator, but crashes every time on the device (iPhone 3GS), right when I take the picture. Is there something wrong with this code? When I Profile with Allocations, active memory is only 3-4 MB when it crashes, so it doesn't seem that the app is running out of memory. I'm using 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];

}
like image 323
soleil Avatar asked Mar 07 '26 02:03

soleil


1 Answers

Does this self.userPicture.image = img; assign the image to an UIImageView?

Before you do that you have to resize it, the ImagePickerController callback gives you an image in JPEG representation, but as soon as you display that image in an UIImageView the data gets decoded to raw format. The 3GS takes pictures with 2048x1536 resolution, which translates into something like 12 MB data, which might already be too much for the 3GS.

There are some categories for resizing available, like this excellent one: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

If you use this just call this before assigning to the imageView:

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

like image 174
Jan Gressmann Avatar answered Mar 09 '26 16:03

Jan Gressmann