views:

174

answers:

2

Hi there,

I have a UIButton which the user clicks on to bring up a UIImagePickerController. Once this has processed it returns an edited UIImage to a delegate handler, which then is supposed to populate the UIButton with the new image.

In practise, however, what happens is if the user selects an image from their library, it works fine. But if they take a picture using the camera and edit it, the image doesn't make it to the UIButton. However... If I put the same image into a UIImageView for test purposes, it shows up.

Moreover, this works fine in the Simulator, but doesn't work on the device. Is it some kind of memory issue? Here's the all-important code:

- (IBAction)takePictureButtonTapped
{
    UIActionSheet *popupQuery = [[UIActionSheet alloc]
                                 initWithTitle:nil
                                 delegate:self
                                 cancelButtonTitle:@"Cancel"
                                 destructiveButtonTitle:nil
                                 otherButtonTitles:@"Take a Photo", @"Upload from Library", nil];

    popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [popupQuery showInView:self.view];
    [popupQuery release];

}

- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"actionSheet:clickedButtonAtIndex:%d", buttonIndex);
    if(buttonIndex == 2)
    {
        // cancel
        [imagePickerController release];
    }
    else
    {
        imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;

        if (buttonIndex == 0)
        {
            // Take a photo
            // Set up the image picker controller and add it to the view
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        else if (buttonIndex == 1)
        {
            // Upload from Library
            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] == NO)
                imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        }


        [self presentModalViewController:imagePickerController animated:YES];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)img
                  editingInfo:(NSDictionary *)editInfo
{
    NSLog(@"imagePickerController::didFinishPickingImage:%@", img);
    itemImage = img;
    [itemImage retain];
    [imageButton setBackgroundImage:itemImage forState:UIControlStateNormal];
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

I've tried setImage, setBackgroundImage, for ALL states, and none of them work. Yet if I put the same image into a UIImageView, it's fine.

Any ideas? I'm stumped!

Thanks!

:-Joe

+1  A: 

Hooray... I answered my own question!

I discovered that the reason it wasn't working properly was because I was using didFinishPickingImage:editingInfo: which is DEPRECATED. I should've been using imagePickerController:didFinishPickingMediaWithInfo: instead. Now it's working perfectly, yay!

For some reason XCode wasn't warning me of the deprecation. Got there in the end!

Joe
A: 

I'm still having the same issue and am using the new delegate method. Still can't figure out why the camera image isn't loading in the button.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [_header.selectPhotoButton setImage:image forState:UIControlStateNormal];
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

_header.selectPhotoButton is my UIButton. Everything works fine when selecting a photo from the camera roll but not at all when the image is taken with the camera.

Colin
You are using the Original Image rather than the edited one. You may have problems with memory because the original image will be in the magnitude of megapixels. Have you tried using UIImagePickerControllerEditedImage instead?
Joe
Also I think you need to put UIImagePicker... inside an NSString wrapper... [info objectForKey:@"UIImagePickerControllerOriginalImage"]
Joe
In this case I wasn't allowing editing so the original image was the one I needed to use. UIImagePickerControllerOriginalImage is a string constant in UIKit so you can just pass it in directly as the key.
Colin
In the end it turned out to be a weird device issue. Everything worked fine on a couple of our test devices. Once I restarted my device everything worked fine. No clue why. But it works!
Colin
Bingo :) glad it now works.
Joe