I have a modal which displays a UIImagePicker and allows the user to take a photo which then gets put into a UIImageView.
Every now and then I get a memory warning ‘Received Memory Warning’ and the UIImageView does not get assigned. The App is fairly simple and it’s not using a lot of memory, and it seems that a lot of the time this has to do with the ImagePicker as a separate process. This only happens when using the camera, and it happens about one in five times.
There’s a lot of talk about this online, and most of the answers say to ‘handle the warning appropriately’; but I”m not sure what that means – I just want the taken photo to show up in the ImageView! It seems to happen before I can do anything about it in the UIImagePicker delegate.
What can I do to mitigate this?
Here’s the didFinishPickingImage Delegate:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo {
//set background for image select button, put image reference somewhere to copy image to documents folder and insert into modular's sentence object.
//ImagePicker.jpg needs it's border set to 1px black.
NSLog(@"imagePickerController: selectedimage: %@", selectedImage);
imageViewBehindPhotoBigButton.clipsToBounds = YES;
[imageViewBehindPhotoBigButton.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageViewBehindPhotoBigButton.layer setBorderWidth: 1.0];
[imageViewBehindPhotoBigButton setBackgroundColor:[UIColor whiteColor]];
[imageViewBehindPhotoBigButton setContentMode: UIViewContentModeScaleAspectFill];
imageViewBehindPhotoBigButton.image = selectedImage;
imageViewBehindPhotoBigButton.hidden = NO;
[choosePhotoBigButton setBackgroundImage:nil forState:UIControlStateNormal];
choosePhotoBarImage.hidden = NO;
choosePhotoText.hidden = NO;
addButton.enabled = YES;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//because iPad uses a pop up - we don't want to dismiss modals here!
[imagePopupController dismissPopoverAnimated:YES];
}else{
[self dismissModalViewControllerAnimated:YES];
}
}
The answer was to implement threading when scaling / handling the image from the UIImagePickerController. There’s a really good discussion that I used here.