使用 gestureRecognizer 设置 UIImageView 的图像

Set image of UIImageView using gestureRecognizer

我想问是否可以使用 gestureRecognizer 设置 UIImageView 的图像,或者是否有必要在 UIImageView 上覆盖一个按钮。

我有一个 UIImageViews 出口集合,称为 imageViews。其中有四个,它们的标签设置为 1 到 4。在我的 viewDidLoad 中,为了将手势识别器添加到集合中的每个图像视图,我使用了:

override func viewDidLoad() {
    super.viewDidLoad()

    for i in (0..<imageViews.count) {

        let imageViewTapped = UITapGestureRecognizer(target: self, action: #selector(selectImage(tap:)))
        imageViewTapped.numberOfTapsRequired = 1
        imageViews[i].addGestureRecognizer(imageViewTapped)

    }

}

接下来,我将我的 gestureRecognizer 创建为 viewController class 中的一个函数。这是创建图像选择器控制器的地方,也是识别被点击的图像视图的地方:

func selectImage(tap: UITapGestureRecognizer) {

    var imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .photoLibrary
    imagePicker.allowsEditing = false
    imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
    self.present(imagePicker, animated: true, completion: nil)

    guard let viewTappedTag = tap.view?.tag else {return}
    self.selectedImageView = imageViews[viewTappedTag - 1] 

}

selectedImageView是viewControllerclass中的变量,类型为UIImageView。我的想法是,这个变量可以保存被点击的 UIImageView,它由 gestureRecognizer 识别,如上所示。然后可以稍后将其传递给委托人。

接下来,我创建了委托 didFinishPickingMediaWithInfo 和 didCancel,尽管为了简洁起见我只展示前者。两者都是在 viewController class:

中创建的
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    var chosenImage = UIImage()
    chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    self.selectedImageView.image = chosenImage

    dismiss(animated: true, completion: nil)

}

不幸的是,点击的图像视图的图像没有更新为从库中选择的图像。这样做的正确方法是什么?而且我觉得我的 viewController 好像在用所有这些代码膨胀,可以将其移至单独的 class 吗?我这样做是不是错了,而应该只覆盖一个按钮?

编辑

到目前为止,我能想到的最好的办法是在我的 imagePickerController 中使用 switch 语句,其中 imageViewOne、imageViewTwo 等是每个 UIImageView 的出口:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    var chosenImage = UIImage()
    chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    switch self.viewTappedTag {
    case 1:
        imageViewOne.image = chosenImage
    case 2:
        imageViewTwo.image = chosenImage
    case 3:
        imageViewThree.image = chosenImage
    case 4:
        imageViewFour.image = chosenImage
    default:
        imageViewOne.image = chosenImage

    }

    dismiss(animated: true, completion: nil)

}

其中 viewTappedTag 是一个 viewController class 变量,其值由被点击的视图的标签定义,并在 gestureRecognizer 中设置。有人可以改进吗?

更新代码如下

在class中声明一个变量如下

var selectedImageViewIndex = 0 //by default selected image view is 0th image view


func selectImage(tap: UITapGestureRecognizer) {

    var imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .photoLibrary
    imagePicker.allowsEditing = false
    imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
    self.present(imagePicker, animated: true, completion: nil)

    guard let viewTappedTag = tap.view?.tag else {return}

    self.selectedImageView = imageViews[viewTappedTag - 1] 

    //Add the following line
    self.selectedImageViewIndex = viewTappedTag - 1

}



func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    var chosenImage = UIImage()
    chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    self.selectedImageView.image = chosenImage

    //Add the following line
    self.imageView[self.selectedImageViewIndex].image = chosenImage
    dismiss(animated: true, completion: nil)

}

尽管我建议不要使用 viewTappedTag - 1,而是从映射对象获取索引值和点击图像标签。

最后我用switch语句达到了目的:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

var chosenImage = UIImage()
chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage

switch self.viewTappedTag {
case 1:
    imageViewOne.image = chosenImage
case 2:
    imageViewTwo.image = chosenImage
case 3:
    imageViewThree.image = chosenImage
case 4:
    imageViewFour.image = chosenImage
default:
    imageViewOne.image = chosenImage

}

dismiss(animated: true, completion: nil)

}