swift - 从库中选择照片后,imagePicker 将不允许您选择另一张照片
问题描述
正如标题所暗示的那样,我正在尝试在您选择一张照片后提供更改照片的可能性。目前,如果用户不取消选择前一张照片,则无法选择另一张照片,因此我对选择多张照片不感兴趣,而只能在每次触摸时移动光标。
fileprivate func showImagePicker(sourceType: UIImagePickerControllerSourceType) {
let imagePickerVC = UIImagePickerController()
imagePickerVC.sourceType = sourceType
imagePickerVC.delegate = self
imagePickerVC.allowsEditing = true
present(imagePickerVC, animated: true, completion: nil)
}
解决方案
The UIImagePickerController
doesn't allow for selecting multiple images. Check the top answer to this question: How to select Multiple images from UIImagePickerController - there is a long list of links for helpful external APIs and libraries. I'll choose the OpalImagePicker for this example. The OpalImagePicker has an option for setting the maximum limit of images a user can select, which means multiple images can be selected. You can make it 10 images, for example, by writing the line imagePicker.maximumSelectionsAllowed = 10
. It is a wonderful API that allows you to customize the image picker to your control quite a bit. It has Swift 4 and Swift 5 compatibility. It even offers delegate methods to pick images from external sources like Facebook, Instagram, or Twitter.
To install, go to CocoaPods, and type in:
pod 'OpalImagePicker'
Sample code:
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")
}
}
推荐阅读
- c# - SQL Server Hierarchyid 和实体框架 HierarchyId 的区别
- java - 如何从第二类的@beforemethod 调用第一类的@Test(POM 数据驱动方法)
- javascript - JavaScript 数组重构
- python - excel保存过程中的字符不正确
- macos - 无法在 macOS 应用程序的 FFMPEG 进程中通过 NSOpenPanel 访问用户选择的文件
- parallel-processing - 循环依赖分析:这个循环可以并行化吗?
- php - 显示下一个字段悬停切换按钮
- python - 如何在python中执行两个大熊猫文件的交叉连接
- html - 为数组对象管理我的 *ngFor 时出现问题
- scala - 播放 Json 验证在日期字段上失败并出现错误:[error.expected.date.isoformat]