我想知道如何使用ImagePickerController
选择多个图像。
但是,我不希望在ImagePickerController
控制器中选择多个图像。相反,我的应用设置在用户选择imageView
的位置(他们都有tapGestureRecogniser
)。有10个。因此,每当用户点击某个imageView
时,我希望将图片应用于所选的imageView
。
但是,它会一直覆盖第一个imageView
。
我该如何解决这个问题?
EDIT ---
想象一下这些是我的imageViews:
[1] [2] [3] [4] [5]
我点击第三个并选择图像。然后应将图像应用于第三个imageView。然后我可以决定选择第一个imageView并添加另一个图像等。
我希望这是有道理的。
编辑x 2 - 我的代码:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleSelectedImage))
selectedImage.addGestureRecognizer(tapGesture)
selectedImage.isUserInteractionEnabled = true
func handleSelectedImage() {
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.mediaTypes = ["public.image", "public.movie"]
pickerController.allowsEditing = true
pickerController.modalPresentationStyle = .popover
pickerController.popoverPresentationController?.delegate = self as! UIPopoverPresentationControllerDelegate
pickerController.popoverPresentationController?.sourceView = selectedImage
let alertController = UIAlertController(title: "Add a Picture", message: "Choose From", preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
pickerController.sourceType = .camera
self.present(pickerController, animated: true, completion: nil)
}
let photosLibraryAction = UIAlertAction(title: "Photos Library", style: .default) { (action) in
pickerController.sourceType = .photoLibrary
self.present(pickerController, animated: true, completion: nil)
}
let savedPhotosAction = UIAlertAction(title: "Saved Photos Album", style: .default) { (action) in
pickerController.sourceType = .savedPhotosAlbum
self.present(pickerController, animated: true, completion: nil)
}
let videoAction = UIAlertAction(title: "Videos", style: .default) { (action) in
pickerController.mediaTypes = ["public.movie"]
pickerController.sourceType = .photoLibrary
self.present(pickerController, animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
alertController.addAction(cameraAction)
alertController.addAction(photosLibraryAction)
alertController.addAction(savedPhotosAction)
alertController.addAction(videoAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
extension AddPostViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIPopoverPresentationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage {
chosenImage = image
selectedImage.image = image
dismiss(animated: true, completion: {
self.performSegue(withIdentifier: "returnHome", sender: nil)
})
}
}
答案 0 :(得分:0)
您可以使用imageView的标记属性和变量来记录哪个imageView进行了调用。下面提到的是粗略的概述。
selectedImageView
)imageView
拨打电话以打开imagePickerController
时,请设置selectedImageView = imageView.tag
selectedImageView
的值,并将图片设置为imageView
,标签相同