为什么我的UIImageView取代了第二个?

时间:2017-06-30 00:17:40

标签: ios swift swift3 uiimageview imageview

我有两个不同UIImageViews的出口,当我选择第一个出现在第一个图像视图上时,但是当我选择第二个图像时,它会替换第一个图像视图,即使它连接到第二个ImageView。这是我选择图像按钮的代码。

@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)

}

2 个答案:

答案 0 :(得分:2)

试试这段代码。所以你需要一个标志来记住点击了哪个图像视图,然后根据它设置图像。

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

继续前进,当您有多个图像视图时,可以使用其他方法来避免在任何地方复制代码。

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

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

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)
}

Handler看起来像这样

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)
}

最后在图片选择委托

func test(v1:Int, v2:Int) {
    a.append((var1: v1, var2: v2, var3: Bool(currentFlag)))
}

答案 1 :(得分:1)

问题是您已重命名了委托方法。如果这样做,将无法识别或调用该方法。

所选答案的另一个选项是扩展UIImageView并使其符合UIImagePickerControllerDelegate / UINavigationControllerDelegate

extension UIImageView: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

     public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
          guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
             //handle error
             return
         }

         image = selectedImage
         picker.presentingViewController?.dismiss(animated: true)
     }

     func presentImagePicker(from viewController: UIViewController) {
         let picker = UIImagePickerController()
         picker.delegate = self
         picker.sourceType = .photoLibrary
         picker.allowsEditing = false

         viewController.present(picker, animated: true)
     }
}

这很不错,因为您可以使用一行为应用中的任何UIImageView启动图像选择器,如下所示:

@IBAction func pickImage1(_ sender: UIButton) {
    myImageView1.presentImagePicker(from: self)
}