如何修改 imagePickerController(源相机)在按下 "Use photo" 直到拍完 4 张照片后不关闭?
How to modify imagePickerController (source camera) not to dismiss after pressing "Use photo" until 4 photos are taken?
我想在 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)
}
很遗憾,这是不可能的。 UIImagePickerController 只能选择一种媒体。如果您想要这种行为,要么必须创建自己的选择器,要么使用库。
如果您想创建自己的,请使用此资源
https://developer.apple.com/documentation/photokit/browsing_and_modifying_photo_albums
它有一个官方演示源,您可以在 2-3 小时内创建自己的演示源(我不得不做一次这个确切的问题,从像选择器这样的网格中选择多个图像和视频)
我会推荐你使用ImagePicker
,它可以通过cocoaPods轻松安装,只需一行代码就可以满足你的要求。
let imagePickerController = ImagePickerController()
imagePickerController.imageLimit = 4
你可以查看ImagePicker
图书馆here
以下是如何使用 ImagePicker
库选择多张图片。
在 Storyboard 中创建一个按钮并在 ViewController 中设置它的出口。
@IBOutlet weak var chooseImage: UIButton!
选中后如果想在VC中看到选中的图片,像这样UIImages
做一个数组
var imageViews:[UIImageView] = []
然后,在viewDidLoad
中,将目标添加到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)
}
}
选择图片并按完成后,您将能够看到类似图片的小缩略图。
我想在 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)
}
很遗憾,这是不可能的。 UIImagePickerController 只能选择一种媒体。如果您想要这种行为,要么必须创建自己的选择器,要么使用库。
如果您想创建自己的,请使用此资源 https://developer.apple.com/documentation/photokit/browsing_and_modifying_photo_albums
它有一个官方演示源,您可以在 2-3 小时内创建自己的演示源(我不得不做一次这个确切的问题,从像选择器这样的网格中选择多个图像和视频)
我会推荐你使用ImagePicker
,它可以通过cocoaPods轻松安装,只需一行代码就可以满足你的要求。
let imagePickerController = ImagePickerController()
imagePickerController.imageLimit = 4
你可以查看ImagePicker
图书馆here
以下是如何使用 ImagePicker
库选择多张图片。
在 Storyboard 中创建一个按钮并在 ViewController 中设置它的出口。
@IBOutlet weak var chooseImage: UIButton!
选中后如果想在VC中看到选中的图片,像这样UIImages
做一个数组
var imageViews:[UIImageView] = []
然后,在viewDidLoad
中,将目标添加到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)
}
}
选择图片并按完成后,您将能够看到类似图片的小缩略图。