Swift - 如何获取 .Mov 格式?
Swift - How to fetch a .Mov format?
我找到了很多关于使用
获取视频或图像的解决方案
phfetchOptions.predicate = NSPredicate(format : "mediaType = %d" , PHAssetMediaType.video.rawvalue)
还有一个 PHAssetMediaSubtype
与我要找的完全不同。
如何准确获取 PHAsset
中所有其他格式的 .Mov 文件的数量
我接近了解决方案,但我想这不是获得计数的正确方法。视频数量创建后台调用数量而不是该过程应该在一个异步调用中完成。有什么建议吗?
private func getMovVideosCount() {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
var count : Int = 0
let imagesAndVideos = PHAsset.fetchAssets(with: fetchOptions)
for i in 0..<imagesAndVideos.count {
let videoRequestOptions = PHVideoRequestOptions()
PHImageManager.default().requestPlayerItem(forVideo: imagesAndVideos[i], options: videoRequestOptions) { (playerItem, result) in
let currentVideoUrlAsset = playerItem?.asset as? AVURLAsset
if let currentVideoFilePAth = currentVideoUrlAsset?.url{
let lastObject = currentVideoFilePAth.pathExtension
if lastObject == "MOV" {
count += 1
}
}
print(Thread.isMainThread) // false
}
}
print(Thread.isMainThread) // true
}
此解决方案给出了.MOV 的计数并解决了调度组的并发问题。
private func getMovVideosCount() {
let fetchOptions = PHFetchOptions()
let dispatchgroup = DispatchGroup()
fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
var count : Int = 0
let imagesAndVideos = PHAsset.fetchAssets(with: fetchOptions)
for i in 0..<imagesAndVideos.count {
dispatchgroup.start()
let videoRequestOptions = PHVideoRequestOptions()
PHImageManager.default().requestPlayerItem(forVideo: imagesAndVideos[i], options: videoRequestOptions) { (playerItem, result) in
let currentVideoUrlAsset = playerItem?.asset as? AVURLAsset
if let currentVideoFilePAth = currentVideoUrlAsset?.url{
let lastObject = currentVideoFilePAth.pathExtension
if lastObject == "MOV" {
count += 1
}
}
print(Thread.isMainThread) // false
dispatchgroup.leave()
}
}
dispatchgroup.notify({})
print(Thread.isMainThread) // true
}
我找到了很多关于使用
获取视频或图像的解决方案phfetchOptions.predicate = NSPredicate(format : "mediaType = %d" , PHAssetMediaType.video.rawvalue)
还有一个 PHAssetMediaSubtype
与我要找的完全不同。
如何准确获取 PHAsset
我接近了解决方案,但我想这不是获得计数的正确方法。视频数量创建后台调用数量而不是该过程应该在一个异步调用中完成。有什么建议吗?
private func getMovVideosCount() {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
var count : Int = 0
let imagesAndVideos = PHAsset.fetchAssets(with: fetchOptions)
for i in 0..<imagesAndVideos.count {
let videoRequestOptions = PHVideoRequestOptions()
PHImageManager.default().requestPlayerItem(forVideo: imagesAndVideos[i], options: videoRequestOptions) { (playerItem, result) in
let currentVideoUrlAsset = playerItem?.asset as? AVURLAsset
if let currentVideoFilePAth = currentVideoUrlAsset?.url{
let lastObject = currentVideoFilePAth.pathExtension
if lastObject == "MOV" {
count += 1
}
}
print(Thread.isMainThread) // false
}
}
print(Thread.isMainThread) // true
}
此解决方案给出了.MOV 的计数并解决了调度组的并发问题。
private func getMovVideosCount() {
let fetchOptions = PHFetchOptions()
let dispatchgroup = DispatchGroup()
fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
var count : Int = 0
let imagesAndVideos = PHAsset.fetchAssets(with: fetchOptions)
for i in 0..<imagesAndVideos.count {
dispatchgroup.start()
let videoRequestOptions = PHVideoRequestOptions()
PHImageManager.default().requestPlayerItem(forVideo: imagesAndVideos[i], options: videoRequestOptions) { (playerItem, result) in
let currentVideoUrlAsset = playerItem?.asset as? AVURLAsset
if let currentVideoFilePAth = currentVideoUrlAsset?.url{
let lastObject = currentVideoFilePAth.pathExtension
if lastObject == "MOV" {
count += 1
}
}
print(Thread.isMainThread) // false
dispatchgroup.leave()
}
}
dispatchgroup.notify({})
print(Thread.isMainThread) // true
}