[英]Swift/XCode - Select ANOTHER using ImagePickerController
我想知道如何使用ImagePickerController
選擇多個圖像。
但是,我不希望在ImagePickerController
控制器中選擇多個圖像。 相反,當用戶選擇我的應用程序設置imageView
(他們都有一個tapGestureRecogniser
)。 有10個。因此,每當用戶點擊某個imageView
,我都希望將圖像應用於所選的imageView
。
但是,它始終覆蓋第一個imageView
。
我該如何解決?
編輯 - -
因此,想象一下這些是我的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)
})
}
}
您可以做的是使用imageView的tag屬性以及一個變量來記錄哪個imageView進行了調用。 下面提到的是一個粗略的輪廓。
selectedImageView
) imageView
進行調用打開imagePickerController
,設置selectedImageView = imageView.tag
selectedImageView
的值並將該圖像設置為具有相同標簽的imageView
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.