Search code examples
objective-cuiimageviewuiimagepickercontroller

Adding a UIImage from the UIImagePickerController to a UIButton


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 my 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?


Solution

  • 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!

    For some reason XCode wasn't warning me of the deprecation.