在UIImagePickerController被解雇之前,我想拍4张照片。如何修改?我有以下按钮动作和委托:
@IBAction func take4Photos(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = .camera;
image.allowsEditing = false
self.present(image, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
imagePicked = image
self.dismiss(animated: true, completion: nil)
}
发布于 2020-06-29 06:24:09
我建议您使用ImagePicker
,它可以通过cocoaPods轻松安装,只需一行代码即可满足您的需求。
let imagePickerController = ImagePickerController()
imagePickerController.imageLimit = 4
您可以查看ImagePicker
库here
下面是如何使用ImagePicker
库来拾取多个图像。
在故事板中创建一个按钮,并将其插座放在ViewController中。
@IBOutlet weak var chooseImage: UIButton!
如果您想在选择后在VC中看到选定的图像,可以像这样创建一个UIImages
数组
var imageViews:[UIImageView] = []
然后,在viewDidLoad
中,单击add target to chooseImage
按钮
chooseImage.addTarget(self, action: #selector(buttonTouched(button:)), for: .touchUpInside)
并在viewDidLoad
外部声明buttonTouched
函数,如下所示
@objc func buttonTouched(button: UIButton) {
let config = Configuration()
config.doneButtonTitle = "Done"
config.noImagesTitle = "Sorry! There are no images here!"
config.recordLocation = false
config.allowVideoSelection = false. //If you don't want video recording
let imagePicker = ImagePickerController(configuration: config)
imagePicker.delegate = self
present(imagePicker, animated: true, completion: nil)
}
最后,在extension
中,遵循如下的ImagePicker
委托函数。
extension OwnerAddListingFacilitiesViewController:ImagePickerDelegate {
func cancelButtonDidPress(_ imagePicker: ImagePickerController) {
imagePicker.dismiss(animated: true, completion: nil)
}
func wrapperDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) { //Don't know what exactly this function does }
func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
for Count in 0..<images.count {
let imageView = UIImageView(image: images[Count])
imageView.frame = CGRect(x: (0 * (110 * Count)), y: 0, width: 50, height: 50)
imageViews.append(imageView)
}
imagePicker.dismiss(animated: true, completion: nil)
}
}
在选择它们并按下完成后,您将能够看到类似于缩略图的小图像。
发布于 2020-06-29 04:54:19
不幸的是,这是不可能的。UIImagePickerController只能选择一种介质。如果你想要这种行为,要么创建你自己的选择器,要么使用一个库。
如果您想创建自己的资源,请使用此资源https://developer.apple.com/documentation/photokit/browsing_and_modifying_photo_albums
它有一个官方的演示源代码,你可以在2-3个小时内创建你自己的(我曾经做过一次这样的问题,从一个类似选择器的网格中选择了多个图像和视频)
https://stackoverflow.com/questions/62627932
复制相似问题