tags:

views:

173

answers:

2

I am having a problem with implementing UIImagePickerController. When the controller loads, it displays modally, and allows the user to select the image. Good so far. Yet, then when it moves to the editing phase, it often displays somewhat corrupted view (the image cropping box is halfway off the top of the screen) and their is no image. It does not crash, but all UI interaction is blocked. The strange part is that this only happens when I compile with Release settings. Under debug compile settings, the image editing works fine! I have tried checking for memory warnings during this time, but none are showing up. Here is the code calling the image picker controller for reference. When I use the camera (the first method), it always works fine. It is just when selecting images from the Library (called from the second method below) does it fail as described above. And again, only on release build, and with various different types of images.

- (IBAction) showCameraController:(id)sender
{
    self.imagePicker =[[UIImagePickerController alloc] init];
    self.imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
    self.imagePicker.delegate=self;
    self.imagePicker.allowsEditing=YES;
    [self presentModalViewController:self.imagePicker animated:YES];
}

- (IBAction) showPictureAlbumController:(id)sender
{
    self.imagePicker =[[UIImagePickerController alloc] init];
    self.imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePicker.delegate=self;
    self.imagePicker.allowsEditing=YES;
    [self presentModalViewController:self.imagePicker animated:YES];
}

The delegate methods are properly implemented, yet, during the problem I am describing, the controller is not yet calling those methods. It is failing when displaying the editing screen before the user is able to select cancel or save. It is just locking up with no crash. Please help!

A: 

If self.imagePicker is a property that retains its value then your code is probably leaking memory. This is probably better:

- (IBAction) showCameraController:(id)sender
{
    UIImagePickerController* imagePicker = [[UIImagePickerController new] autorelease];
    if (imagePicker != nil) {
        imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate=self;
        imagePicker.allowsEditing=YES;
        [self presentModalViewController: imagePicker animated: YES];
    }
}

You don't have to keep the imagePicker around since you will be passed a reference to it by the delegate methods.

St3fan
Thanks, but this is not solving the problem. I originally had tried with retain on the property and including an autorelease as shown. When I had posted the code I was just checking to see if there was some odd release happening and took off the autorelease. The only other reference to the imagePicker is in dealloc of the controller containing this code, and this is not getting hit when the problem is occuring. There is not really much else happening in this class so I am quite sure it is not a memory management problem with the imagePicker. Any other thoughts?
Greg Reichow
Well, run it under Instruments then. To check where the memory is going. It should give you at least some hint.
St3fan
A: 

Solved the problem.

When the app loaded, I was building the UITabBarController in code and then added its view to the window. Yet, I had forgot to make the main window key and visible just after that:

[window makeKeyAndVisible]

Interestingly the entire app worked fine, yet, it caused the problem described above. Adding this in fixed the problem.

Greg Reichow