views:

327

answers:

4

Hi, it is very strange, because this error doesn't happen all the time...

I have the following code:

- (IBAction)getPhoto:(id)sender {
  UIImagePickerController * picker = [[UIImagePickerController alloc] init];
  picker.delegate = self;   
#if TARGET_IPHONE_SIMULATOR
  picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;    
#else   
  picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
#endif
  [self presentModalViewController:picker animated:YES];
}

with the corresponding delegated selector

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  UIImage *image = [[info objectForKey:@"UIImagePickerControllerOriginalImage"] imageByScalingToSize:CGSizeMake(480, 320)];
  [[self imageView] setImage:image];
  [picker dismissModalViewControllerAnimated:YES];
}

strange thing is, somtimes the image appears in imageView and sometimes not. I have called
(gdb) po UIImagePNGRepresentation(image)
right after setImage and stuffed the output into a file. The file is a perfect png.

Has anyone experienced the same? Has anyone found a solution for it?

Thanks in advance

Max

+1  A: 

Sounds like you are running out of memory.

I think you are supposed to release the UIImagePickerController after putting it on screen with presentModalViewController:.

(It will be retained for you by the view controller that is presenting it)

St3fan
You are right, the release should be there, but that is not the solution. It still does not work. And as said before, the image is there, I can po the Data of the PNG Representation, store it in a file and it is a real png file. And I don't receive any "out of memory error" or exceptions. The image is just not presented
maxbareis
A: 

The solution is:

Right after taking the picture the receivedMemoryWarning in the controller is called by the system. But the controller itself remains retained. When pressing use the delegate selector is performed and the image in imageView is set. But after dismissing the modal view the system reinitializes the view from the nib. So the image from nib overwrites the image set in the delegate selector. Solution is to store the contents of all text Fields and views in didReceiveMemoryWarning in a Dictionary, and storing the image in a instance variable and resetting them in viewDidLoad if they are present.

maxbareis
A: 

Yes that's the solution that worked for me. Using didreceivememorywarning to store all textfield and uiimage data to the phone. On pickerdidfinish I also save the image to the phone. Onload I check if that image file name exists in the phone, if so load it. Basically the iPhone os is closing your app and reopennkng it. Put some NSLog in viewDidLoad and you will notice that gets called during the image taking process. That method is only supposed to get called once. If it is getting called Again then it means your application died and restarted. It is a memory issue. Not enough horse power. I don't see this issue on the iPhone 3GS

Al
A: 

I had this problem even on the 3GS. But I could solve it, by releasing the UIImagePickerViewController before setting the imageView.image I guess this freed a couple of bytes. Maybe this info helps.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
 [self dismissModalViewControllerAnimated:YES];
 [picker release];
 UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
 imageView.image = image;
}
Michael