Swift / XCode - 使用ImagePickerController

时间:2017-06-14 00:34:55

标签: ios swift xcode

我想知道如何使用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)
            })
        }



    }

1 个答案:

答案 0 :(得分:0)

您可以使用imageView的标记属性和变量来记录哪个imageView进行了调用。下面提到的是粗略的概述。

  • 创建一个变量(比如selectedImageView
  • 将图像视图的标记属性设置为1-5
  • imageView拨打电话以打开imagePickerController时,请设置selectedImageView = imageView.tag
  • 挑选图片时,请检查selectedImageView的值,并将图片设置为imageView,标签相同