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

时间:2017-04-26 23:17:08

标签: ios objective-c uiimageview uiimagepickercontroller

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

1 个答案:

答案 0 :(得分: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;
}