我有 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/