ios - How to pick multiple videos as well as photos from gallery using Swift
问题描述
I'm trying to pick both photos and videos from gallery.
I have done a few things like picked photos but not showing the videos. I have used BSImagepicker library and I have't found any video related thing in this library. If anyone have done such kind of work or know about any other library which picks both photos and videos please refer.
import UIKit
import Photos
import BSImageView
import BSImagePicker
class ViewController: UIViewController {
let pickerController = BSImagePickerViewController()
var SelectedAssets = [PHAsset]()
var PhotoArray = [UIImage]()
@IBOutlet var imageView: UIImageView!
@IBAction func buttonAction(_ sender: Any) {
pickImages()
}
func pickImages() {
self.bs_presentImagePickerController(pickerController, animated: true, select: { (asset: PHAsset) in
}, deselect: { (assets: PHAsset) in
}, cancel: { (assets: [PHAsset]) in
}, finish: { (assets: [PHAsset]) in
for i in 0..<assets.count {
self.SelectedAssets.append(assets[i])
}
self.convertAssetToImages()
}, completion: nil)
}
func convertAssetToImages(){
if SelectedAssets.count != 0 {
for i in 0..<SelectedAssets.count {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var thumbnail = UIImage()
option.isSynchronous = true
manager.requestImage(for: SelectedAssets[i], targetSize: CGSize(width: 500, height: 500), contentMode: .aspectFit, options: option, resultHandler: { (result, info) in
thumbnail = result!
})
let data = UIImageJPEGRepresentation(thumbnail, 0.7)
let newImage = UIImage(data: data!)
self.PhotoArray.append(newImage! as UIImage)
}
hcArray = PhotoArray
self.imageView.animationImages = self.PhotoArray
self.imageView.animationDuration = 3.0
self.imageView.startAnimating()
}
}
}
解决方案
你是对的,图书馆的设计不是很好......
在这里,刚出炉: https ://cocoapods.org/pods/CameraRoll
我将添加自述文件并对其进行改进,但效果很好。
首先,将此添加到您的 plist
<key>NSPhotoLibraryUsageDescription</key>
<string>To Find images</string>
然后导入
import CameraRoll
import Photos
那么当你想使用它时,
let cr = CameraRoll()
// For pitures use .Image mode
cr.present(in: self, mode: .Video) { (assets) in
// Returns array of PHAsset
}
}
使用辅助函数进行转换
let cr = CameraRoll()
cr.present(in: self, mode: .Image) { (assets) in
if let assets = assets, let first = assets.first {
DispatchQueue.main.async {
AssetManager.sharedInstance.getImageFromAsset(phAsset: first) { (image) in
self.imageView.image = image
}
}
}
}
推荐阅读
- java - 当我通过 java 代码运行 jmeter 时出现错误 converters.ConversionException
- java - Java 中的 System.in.read() 阻塞多长时间
- mysql - 如何使这个 MySQL 查询(带有子查询)更快?
- c# - XmlDocument,预格式化的 xml:仅在 XML 声明后添加新行
- matlab - 如何使用 fft 找到周期信号的最大频率?
- python - 有没有更好的方法来标记一些字符串?
- c# - 如何更改通过 PrefabUtility.InstantiatePrefab 实例化的 GameObject 的父级?
- c# - 尝试选择单个节点会返回错误“表达式必须计算为节点集”。
- r - ifelse 语句从同一 df 行返回值
- python-3.x - 是否可以为无监督学习自定义 tf.estimator,自定义评估图没有损失?