一个UIImagePickerController用于多个ImageVIew

时间:2018-09-08 19:48:02

标签: ios swift swift4 uiimagepickercontroller

请您帮我UIImagePickerController

我在1个ViewController中有12个ImageView。我需要帮助,以便每个ImageView可以从媒体库中选择不同的照片。有人告诉我要为他们使用标签,但由于我是Swift新手,所以无法使用它。

2 个答案:

答案 0 :(得分:2)

为情节提要中的每个UIImageView启用User Interaction Enabled

enter image description here

向每个TapGestureRecogniser添加UIImageView。将每个TapGestureRecogniserIBAction连接起来。

@IBAction func tap(_ sender: UITapGestureRecognizer) {

    currentImageView = sender.view as! UIImageView

    let picker = UIImagePickerController()
    picker.delegate = self
    self.present(picker, animated: true, completion: nil)

}

定义变量以存储当前的UIImageView

private var currentImageView: UIImageView? = nil

处理图像选择并将图像分配给currentImageView

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

    let image = info[UIImagePickerController.InfoKey.originalImage]
    currentImageView?.image = image as! UIImage

    self.dismiss(animated: true, completion: nil)
}

答案 1 :(得分:0)

这与迅速无关。您可以在“界面”构建器(或代码)中设置一个“标签”号。UIResponder后代中有一个.tag属性。 之后,您可以在回调方法中进行切换,例如

使其更加清晰:

let picker: UIImagePicker! = { 
    // setup your image picker here, don't forget to set the proper delegate
}()

var lastSender: AnyObject?
func onTouch(_ sender: AnyObject?) {
    // add checks for the sender here
    lastSender = sender
    present(picker, animated: true, completion: *Your Completion*)
}

委托:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])

let image = *Get Image From Info* lastSender?.image = image