为什么我的UIImageView替换第二个? [英] Why does my UIImageView replace the second one?

查看:229
本文介绍了为什么我的UIImageView替换第二个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为两个不同的UIImageViews有两个插座,当我选择第一个时,它将出现在第一个Image View中,但是当我选择第二个Image时,即使它已连接到第二个ImageView,它也会替换第一个Image View.这是我的选择图像"按钮的代码.

I have two outlets for two different UIImageViews, when I select the first one it'll appear on the first Image View but when I select the second Image, it replaces the first Image View even though it's connected to the second ImageView. This is my code for the Select Image button.

@IBOutlet weak var myImageView1: UIImageView!
@IBOutlet weak var myImageView2: UIImageView!


@IBAction func pickImage1(_ sender: Any) {

    let image = UIImagePickerController()
    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.photoLibrary
    image.allowsEditing = false

    self.present(image, animated: true)
}

//Add didFinishPickingMediaWithInfo here
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        myImageView1.image = image
    }
    else {
        //error
    }
    self.dismiss(animated: true, completion: nil)
}


@IBAction func pickImage2(_ sender: Any) {
    let image2 = UIImagePickerController()
    image2.delegate = self
    image2.sourceType = UIImagePickerControllerSourceType.photoLibrary
    image2.allowsEditing = false

    self.present(image2, animated: true)
}

//Add didFinishPickingMediaWithInfo here
func imagePickerController2(_ picker2: UIImagePickerController, didFinishPickingMediaWithInfo2 info2: [String : Any]) {
    if let image2 = info2[UIImagePickerControllerOriginalImage] as? UIImage {
        myImageView2.image = image2
    }
    else {
        //error
    }
    self.dismiss(animated: true, completion: nil)

}

推荐答案

尝试此代码.因此,您需要一个标记来记住单击哪个图像视图,然后在该图像上设置图像.

Try this code. So you need a flag to remember which image view is clicked, and then set image base on that.

var selected = 1

@IBAction func pickImage1(_ sender: Any) {

    let image = UIImagePickerController()
    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.photoLibrary
    image.allowsEditing = false
    selected = 1

    self.present(image, animated: true)
}

//Add didFinishPickingMediaWithInfo here
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        if selected == 1 {
            myImageView1.image = image
        } else {
            myImageView2.image = image
        }
    }
    else {
        //error
    }
    self.dismiss(animated: true, completion: nil)
}


@IBAction func pickImage2(_ sender: Any) {
    let image2 = UIImagePickerController()
    image2.delegate = self
    image2.sourceType = UIImagePickerControllerSourceType.photoLibrary
    image2.allowsEditing = false
    selected = 2

    self.present(image2, animated: true)
}

向前移动,当您拥有多个图像视图时,可以使用另一种方法来避免在各处复制代码.

Moving forward, when you have multiple image views, you can use another method to avoid copying code everywhere.

首先,为每个图像视图添加唯一的标签.避免使用0,因为默认标记为0.因此,您将看到带有标记为1至4的图像视图.

First, add an unique tag for each image view. Avoid using 0 because the default tag is 0. So you will have image views with tags say 1 to 4.

对所有图像视图调用相同的方法,以便通过单击任意一个图像来触发此功能

Call this same method to all your image views so that this function is trigger by clicking on any of them

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.addGestureRecognizer(tapGestureRecognizer)

处理程序看起来像这样

func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
    let image = UIImagePickerController()
    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.photoLibrary
    image.allowsEditing = false
    let tappedImage = tapGestureRecognizer.view as! UIImageView
    selected = tappedImage.tag
    self.present(image, animated: true)
}

最后是图像拾取委托

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        if let imageView = self.view.viewWithTag(selected) as? UIImageView {
            imageView.image = image
        }
    }
    else {
        //error
    }
    self.dismiss(animated: true, completion: nil)
}

这篇关于为什么我的UIImageView替换第二个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆