ios - 使 UIImagePickerController 将所选图像设置为多个 UIImageViews

标签 ios objective-c uiimageview uiimagepickercontroller

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

最佳答案

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

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

button1.tag = 1;

要将当前标签使用变量存储在 Controller 中:
@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;
}

关于ios - 使 UIImagePickerController 将所选图像设置为多个 UIImageViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43646083/

相关文章:

ios - 将结构添加到字典中,然后随机调用这些结构

iphone - 以编程方式将数据从 JSON 加载到对象?

ios - 为什么 AVAssetWriter 会膨胀视频文件?

ios - 释放什么

ios - 如何创建以 TabBarViewController 为根的导航堆栈?

ios - 移动图像时更改像素颜色

ios - 加速第一个 UIImageView 动画(强制缓存图像)

ios - 可以[self.window makeKeyAndVisible];在设置 rootviewcontroller 之前被调用

objective-c - 标记实例变量@private

uitableview - 使用 Storyboard中的 SDWebImage 在自动布局中查看图像