【问题标题】:imagePicker once you select a photo from library doesn't allow you to select another one从库中选择照片后,imagePicker 将不允许您选择另一张照片
【发布时间】:2019-10-28 09:24:40
【问题描述】:

正如标题所暗示的那样,我正在尝试在您选择一张照片后提供更改照片的可能性。 目前,如果用户不取消选择前一张照片,则无法选择另一张照片,因此我对选择多张照片不感兴趣,而只能在每次触摸时移动光标。

fileprivate func showImagePicker(sourceType: UIImagePickerControllerSourceType) {
        let imagePickerVC = UIImagePickerController()

        imagePickerVC.sourceType = sourceType
        imagePickerVC.delegate = self
        imagePickerVC.allowsEditing = true

        present(imagePickerVC, animated: true, completion: nil)
}

【问题讨论】:

  • 你不能,你必须创建一个自定义视图,在集合视图中显示照片中的媒体,并对这些项目应用多项选择

标签: swift xcode imagepicker


【解决方案1】:

UIImagePickerController 不允许选择多个图像。检查此问题的最佳答案:How to select Multiple images from UIImagePickerController - 有很多有用的外部 API 和库的链接。我将为此示例选择OpalImagePicker。 OpalImagePicker 有一个选项用于设置用户可以选择的图像的最大限制,这意味着可以选择多个图像。例如,您可以通过编写imagePicker.maximumSelectionsAllowed = 10 行使其包含 10 个图像。这是一个很棒的 AP​​I,允许您根据自己的控件自定义图像选择器。它具有 Swift 4 和 Swift 5 的兼容性。它甚至提供委托方法来从 Facebook、Instagram 或 Twitter 等外部资源中挑选图像。

要安装,请转到CocoaPods,然后输入:

pod 'OpalImagePicker'

示例代码:

import OpalImagePicker

class DelegateExampleViewController: UIViewController {
    @IBAction func photoLibraryTapped(_ sender: UIButton) {
        let imagePicker = OpalImagePickerController()
        imagePicker.imagePickerDelegate = self
        imagePicker.selectionTintColor = UIColor.white.withAlphaComponent(0.7)
        imagePicker.selectionImageTintColor = UIColor.black
        imagePicker.selectionImage = UIImage(named: "x_image")  // Change image to X rather than checkmark
        imagePicker.statusBarPreference = UIStatusBarStyle.lightContent
        imagePicker.maximumSelectionsAllowed = 10  // This is the line that relates to your question
        let configuration = OpalImagePickerConfiguration()
        configuration.maximumSelectionsAllowedMessage = NSLocalizedString("You cannot select that many 
             images!", comment: "")
        imagePicker.configuration = configuration
        present(imagePicker, animated: true, completion: nil)
    }
}

extension DelegateExampleViewController: OpalImagePickerControllerDelegate {
    func imagePickerDidCancel(_ picker: OpalImagePickerController) {
        // Cancel action
    }

    func imagePicker(_ picker: OpalImagePickerController, didFinishPickingImages images: [UIImage]) {
        // Save Images, update UI   
        // Dismiss Controller
        presentedViewController?.dismiss(animated: true, completion: nil)
    }

    func imagePickerNumberOfExternalItems(_ picker: OpalImagePickerController) -> Int {
        return 1
    }

    func imagePickerTitleForExternalItems(_ picker: OpalImagePickerController) -> String {
        return NSLocalizedString("External", comment: "External (title for UISegmentedControl)")
    }

    func imagePicker(_ picker: OpalImagePickerController, imageURLforExternalItemAtIndex index: Int) -> URL? {
        return URL(string: "https://placeimg.com/500/500/nature")
    }
}

【讨论】:

  • 正如我所说:所以我对选择多张照片不感兴趣,而只能在每次触摸时移动光标。
  • 我对“每次触摸都能移动光标”的意思有点困惑
  • 当用户在相机库中时,他可以选择一张照片,并且该项目上会出现一个复选标记,如果他想更改选择的项目(照片),他不能。唯一的方法是取消选择之前选择的项目,然后选择另一个项目。
猜你喜欢
  • 1970-01-01
  • 2016-09-23
  • 2016-07-04
  • 1970-01-01
  • 2018-10-04
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
相关资源
最近更新 更多