Ios 选择照片后,如何知道要更新哪个图像视图?

Ios 选择照片后,如何知道要更新哪个图像视图?,ios,swift,uiview,Ios,Swift,Uiview,我有一个带有2个UIImageView和2个按钮的视图,以便使用UIImagePickerViewController选择2个不同的图片。它实际上可以工作,但当我上传一个图像时,另一个UIView图像也选择了相同的图像 我做错了什么 } @IBAction func uploadButton(_ sender: Any) { let imagePickerController = UIImagePickerController() imagePickerController.

我有一个带有2个UIImageView和2个按钮的视图,以便使用UIImagePickerViewController选择2个不同的图片。它实际上可以工作,但当我上传一个图像时,另一个UIView图像也选择了相同的图像

我做错了什么

}

@IBAction func uploadButton(_ sender: Any) {

    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self

    let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)

    actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in

        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            imagePickerController.sourceType = .camera
            self.present(imagePickerController, animated: true, completion: nil)
        }else{
            print("Camera not available")
        }


    }))

    actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in
        imagePickerController.sourceType = .photoLibrary
        self.present(imagePickerController, animated: true, completion: nil)
    }))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(actionSheet, animated: true, completion: nil)


}

@IBAction func coverButton(_ sender: Any) {

    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self

    let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)

    actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in

        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            imagePickerController.sourceType = .camera
            self.present(imagePickerController, animated: true, completion: nil)
        }else{
            print("Camera not available")
        }


    }))

    actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in
        imagePickerController.sourceType = .photoLibrary
        self.present(imagePickerController, animated: true, completion: nil)
    }))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(actionSheet, animated: true, completion: nil)




}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage

    profileImage.image = image
    backgroundImage.image = image

    picker.dismiss(animated: true, completion: nil)

}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: nil)

}

您有重复的代码,无法确定在选择/拍摄照片后更新哪个图像视图。你需要重构你的代码

将属性添加到类:

var chosenImageView: UIImageView?
然后将您的通用代码放入它自己的方法中:

function selectPhoto() {
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self

    let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)

    if UIImagePickerController.isSourceTypeAvailable(.camera) {
        actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in
            imagePickerController.sourceType = .camera
            self.present(imagePickerController, animated: true, completion: nil)
        }))
    }else{
        print("Camera not available")
    }

    actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in
        imagePickerController.sourceType = .photoLibrary
        self.present(imagePickerController, animated: true, completion: nil)
    }))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(actionSheet, animated: true, completion: nil)
}
请注意,如果没有摄像头,则不会显示摄像头按钮

然后重构您的两个操作:

@IBAction func uploadButton(_ sender: Any) {
    chosenImageView = profileImage

    selectPhoto()
}

@IBAction func coverButton(_ sender: Any) {
    chosenImageView = backgroundImage

    selectPhoto()
}
最后是委托方法:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage

    chosenImageView?.image = image

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

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: nil)
}

您总是在imagePickerController didFinishPickingMediaWithInfo方法中将图像设置为两个视图。我应该替换什么,谢谢您的回答