问题
I am trying to choose and display multiple images (approx. 5 images) but for some reason I am only able to select and display a single image at a time. I have tried to implement the below code for displaying multiple images. Please help guys. Thanks in advance.
import UIKit
import Alamofire
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let imagesData = [UIImage]()
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var myImageView1: UIImageView!
@IBOutlet weak var myImageView2: UIImageView!
@IBOutlet weak var myImageView3: UIImageView!
@IBOutlet weak var myImageView4: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
myImageView.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
myImageView1.backgroundColor = #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)
myImageView2.backgroundColor = #colorLiteral(red: 0.06274510175, green: 0, blue: 0.1921568662, alpha: 1)
myImageView3.backgroundColor = #colorLiteral(red: 0.5791940689, green: 0.1280144453, blue: 0.5726861358, alpha: 1)
myImageView4.backgroundColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
myImageView.image = image
myImageView.contentMode = .scaleAspectFill
myImageView1.image = image
myImageView1.contentMode = .scaleAspectFill
myImageView2.image = image
myImageView2.contentMode = .scaleAspectFill
myImageView3.image = image
myImageView3.contentMode = .scaleAspectFill
myImageView4.image = image
myImageView4.contentMode = .scaleAspectFill
dismiss(animated: true, completion: nil)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let controller = UIImagePickerController()
controller.delegate = self
controller.sourceType = .photoLibrary
present(controller, animated: true, completion: nil)
}
}
回答1:
You are using UIImagePickerController and it don't allow multiple selection of images.
If you want to select multiple images at a time then you need to create your own custom image picker controller.
Another way is that, when user select an image the set that image in imageview1 and present image picker controller again to select 2nd image, and so on...until your image count reaches 5.
回答2:
There are many third party libraries out there but BSImagePicker helped me resolve my problem and I have been using this library for a while now.
Here's the link -> https://github.com/mikaoj/BSImagePicker
Hope it helps you as well!
来源:https://stackoverflow.com/questions/49230181/select-multiple-images-and-display-them-in-image-views