【问题标题】:Make UIImagePickerController set selected image to multiple UIImageViews使 UIImagePickerController 将所选图像设置为多个 UIImageViews
【发布时间】:2017-04-26 23:17:08
【问题描述】:

我有 3 个按钮和 3 个 UIImageView,所有按钮都打开一个 UIImagePickerController。我希望按钮 1 打开 UIImagePickerController,然后将所选图像设置为 UIImageView 1,按钮 2 打开 UIImagePickerController,并将所选图像设置为 UIImageView 2,等等。如何才能做到这一点?谢谢!

【问题讨论】:

  • 有很多方法可以做到这一点。请展示您尝试过的方法。您可以使用布尔值来确保单击按钮 1,在这种情况下,您选择图像后,它将被添加到 imageView1。剩下的两个也一样!

标签: ios objective-c uiimageview uiimagepickercontroller


【解决方案1】:

您可以为按钮设置标签 (1,2,3) 以确定用户按下了哪个按钮。将此值存储到某个变量中,稍后使用它来确定您需要将从 UIImagePickerController 接收的 UIImage 设置到哪个 UIImageView 中。

更新。 标签可以直接在 Interface Bulder 或代码中设置:

button1.tag = 1;

在控制器中存储当前标签使用变量:

@interface NameOfYourController : UIViewController
{
    NSInteger currentTag;
}

要获取按钮标签,请在事件内部的按钮修饰中使用它:

-(void)buttonTap:(UIButton *)sender
{
    currentTag = sender.tag;
}

当 UIImagePickerController 调用媒体拾取完成时,使用存储标签:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *img = info[UIImagePickerControllerOriginalImage];
    if (currentTag==1)
        imageView1.image = image;
    else if (currentTag==2)
        imageView2.image = image;
    else if (currentTag==3)
        imageView3.image = image;
}

【讨论】:

  • 你能给我提供一个代码示例,用于将这些标签设置为按钮,以及 id 如何使用所述标签来选择要设置的 UIImageView 的示例吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
相关资源
最近更新 更多