【问题标题】:Adding a UIImage from the UIImagePickerController to a UIButton将 UIImagePickerController 中的 UIImage 添加到 UIButton
【发布时间】:2010-07-29 09:59:35
【问题描述】:

我有一个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,那就没问题了。

有什么想法吗?

【问题讨论】:

  • 嘿...我为这个问题赢得了“风滚草”徽章...没有人有任何想法吗?

标签: objective-c uiimageview uiimagepickercontroller


【解决方案1】:

我发现它无法正常工作的原因是因为我使用了已弃用的didFinishPickingImage:editingInfo:。我应该改用imagePickerController:didFinishPickingMediaWithInfo:。现在它运行良好!

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

【讨论】:

    猜你喜欢
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    相关资源
    最近更新 更多