将 UIImage 从 UIImagePickerController 添加到 UIButton

发布于 2024-09-11 18:19:09 字数 2655 浏览 3 评论 0原文

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

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心奴独伤 2024-09-18 18:19:09

我发现它无法正常工作的原因是因为我使用的是已弃用的 didFinishPickingImage:editingInfo: 。我应该使用 imagePickerController:didFinishPickingMediaWithInfo: 来代替。现在一切正常!

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文