如何用photokit获取图片库中所有视频文件的url
How to get the url of all video files stored in photo library with photokit
我找到了很多解决方案,但都是用ALAssetLibrary实现的,但是assetlibrary会在iOS9中贬值,我不知道如何获取照片库中的视频文件,并且获取他们的 url 和缩略图。
您可以使用 Apple 为照片套件提供的 sample 代码
代码类似于
PHPhotoLibrary.requestAuthorization { (status) -> Void in
let allVidOptions = PHFetchOptions()
allVidOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.Video.rawValue)
allVidOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let allVids = PHAsset.fetchAssetsWithOptions(allVidOptions)
for index in 0..<allVids.count {
//fetch Asset here
print(allVids[index])
}
}
您还可以使用媒体类型为 fetchAssets 的函数
视频
let videosArray = PHAsset.fetchAssets(with: .video, options: nil)
图片
let imagesArray = PHAsset.fetchAssets(with: .image, options: nil)
我找到了很多解决方案,但都是用ALAssetLibrary实现的,但是assetlibrary会在iOS9中贬值,我不知道如何获取照片库中的视频文件,并且获取他们的 url 和缩略图。
您可以使用 Apple 为照片套件提供的 sample 代码
代码类似于
PHPhotoLibrary.requestAuthorization { (status) -> Void in
let allVidOptions = PHFetchOptions()
allVidOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.Video.rawValue)
allVidOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let allVids = PHAsset.fetchAssetsWithOptions(allVidOptions)
for index in 0..<allVids.count {
//fetch Asset here
print(allVids[index])
}
}
您还可以使用媒体类型为 fetchAssets 的函数
视频
let videosArray = PHAsset.fetchAssets(with: .video, options: nil)
图片
let imagesArray = PHAsset.fetchAssets(with: .image, options: nil)