0

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

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

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

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

1 回答 1

0

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")
    }
}
于 2019-10-28T10:02:29.377 回答