gpt4 book ai didi

swift - imagePicker 一旦您从库中选择一张照片,就不允许您选择另一张照片

转载 作者:行者123 更新时间:2023-11-30 10:33:28 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

UIImagePickerController 不允许选择多个图像。查看此问题的最佳答案:How to select Multiple images from UIImagePickerController - 有一个很长的链接列表,提供有用的外部 API 和库。我会选择OpalImagePicker对于这个例子。 OpalImagePicker 有一个选项用于设置用户可以选择的图像的最大限制,这意味着可以选择多个图像。例如,您可以通过编写行 imagePicker.maximumSelectionsAllowed = 10 将其设置为 10 个图像。这是一个很棒的 AP​​I,允许您根据自己的控件自定义图像选择器。它具有 Swift 4 和 Swift 5 兼容性。它甚至提供委托(delegate)方法来从 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")
}
}

关于swift - imagePicker 一旦您从库中选择一张照片,就不允许您选择另一张照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58588395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com