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];
}
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With