【问题标题】:How to modify imagePickerController (source camera) not to dismiss after pressing "Use photo" until 4 photos are taken?如何修改imagePickerController(源相机)在按下“使用照片”后不关闭,直到拍摄4张照片?
【发布时间】:2020-06-28 20:34:01
【问题描述】:

我想在 UIImagePickerController 被解除之前拍摄 4 张照片。如何修改它? 我有以下按钮操作和委托:

@IBAction func take4Photos(_ sender: Any) {
                if UIImagePickerController.isSourceTypeAvailable(.camera) {
                    let image = UIImagePickerController()
                    image.delegate = self
                    image.sourceType = .camera;
                    image.allowsEditing = false
                    self.present(image, animated: true, completion: nil)
                    
                }
            }


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
       
        imagePicked = image
       
        self.dismiss(animated: true, completion: nil)
        
    }

【问题讨论】:

    标签: swift xcode uiimagepickercontroller


    【解决方案1】:

    我会推荐你​​使用ImagePicker,它可以通过cocoaPods轻松安装,只需一行代码就可以满足你的要求。

    let imagePickerController = ImagePickerController()
    imagePickerController.imageLimit = 4
    

    您可以查看ImagePickerhere

    这里介绍了如何使用 ImagePicker 库来选择多个图像。

    在 Storyboard 中创建一个按钮,并在 ViewController 中有它的出口。

    @IBOutlet weak var chooseImage: UIButton!
    

    如果你想在选择后在VC中看到选择的图像,像这样制作一个UIImages的数组

    var imageViews:[UIImageView] = []
    

    然后,在viewDidLoad 中,将目标添加到chooseImage 按钮

    chooseImage.addTarget(self, action: #selector(buttonTouched(button:)), for: .touchUpInside)
    

    并像这样在viewDidLoad之外声明buttonTouched函数

      @objc func buttonTouched(button: UIButton) {
      let config = Configuration()
      config.doneButtonTitle = "Done"
      config.noImagesTitle = "Sorry! There are no images here!"
      config.recordLocation = false 
      config.allowVideoSelection = false. //If you don't want video recording
    
      let imagePicker = ImagePickerController(configuration: config)
      imagePicker.delegate = self
    
      present(imagePicker, animated: true, completion: nil)
    }
    

    然后,最后,在extension 内部,像这样符合ImagePicker 委托函数。

    extension OwnerAddListingFacilitiesViewController:ImagePickerDelegate {
    
     func cancelButtonDidPress(_ imagePicker: ImagePickerController) {
        
        imagePicker.dismiss(animated: true, completion: nil)
      }
    
    
     func wrapperDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) { //Don't know what exactly this function does }
    
     func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
        for Count in 0..<images.count {
            let imageView = UIImageView(image: images[Count])
            imageView.frame = CGRect(x: (0 * (110 * Count)), y: 0, width: 50, height: 50)
            imageViews.append(imageView)
        }
        
            imagePicker.dismiss(animated: true, completion: nil)
      }
    
     }
    

    选择它们并按完成后,您将能够看到类似小缩略图的图像。

    【讨论】:

    • 嗨,我通过可可豆荚安装了pod 'ImagePicker',但由于某种原因,我的 VC 中没有可用的 ImagePickerDelegate(尽管它在豆荚中声明).. 有什么提示吗?:)
    • 嗨,当我执行您的步骤时,我收到 3 个警告 1)使用未解析的标识符配置 2)使用... ImagePickerController 和 3)使用未声明的类型 OwnerAddListingFacilitiesViewController ...正如我之前所说,由于某些原因,ImagePicker 的方法不可用,不知道为什么
    • @Eljer,您是否在文件中导入了“ImagePicker”窗格?在 ViewController 的顶部写 import ImagePicker。对于您提到的第三个错误,那是因为 OwnerAddListingFacilitiesViewController 是我个人在代码中使用的 ViewController 文件的名称。您必须在此处提及您的 viewController 的名称。
    • 完美!这正是我想要的。谢谢。
    【解决方案2】:

    很遗憾,这是不可能的。 UIImagePickerController 只能选择一种媒体。如果您想要这种行为,您必须创建自己的选择器或使用库。

    如果您想创建自己的,请使用此资源 https://developer.apple.com/documentation/photokit/browsing_and_modifying_photo_albums

    它有一个官方演示源,您可以在 2-3 小时内创建自己的演示源(我必须做一次这个确切的问题,从像选择器的网格中选择多个图像和视频)

    【讨论】:

      猜你喜欢
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多