将UIImage从UIImagePickerController添加到UIButton

时间:2010-07-29 09:59:36

标签: objective-c uiimageview uiimagepickercontroller

我有一个UIButton用户点击它以显示UIImagePickerController。处理完成后,它会将已编辑的UIImage返回给委托处理程序,然后该处理程序将使用新映像填充UIButton

然而,实际上,如果用户从他们的库中选择一个图像,它会正常工作。但如果他们使用相机拍摄照片并对其进行编辑,则图像无法显示UIButton。但是,如果我将相同的图像放入UIImageView进行测试,则会显示出来。

此外,这在模拟器中工作正常,但在设备上不起作用。这是某种记忆问题吗?这是我的代码:

- (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];
}

我为所有州尝试了setImagesetBackgroundImage,但没有一个能够奏效。然而,如果我将相同的图像放入UIImageView,那很好。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我发现它无法正常工作的原因是因为我使用的是didFinishPickingImage:editingInfo:已弃用。我应该一直在使用imagePickerController:didFinishPickingMediaWithInfo:。现在它完美运作了!

出于某种原因,XCode没有警告我弃用。