gpt4 book ai didi

ios - Swift - 如何获取 .Mov 格式?

转载 作者:行者123 更新时间:2023-11-28 13:54:04 28 4
gpt4 key购买 nike

我找到了很多关于使用

获取视频或图像的解决方案
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的计数并解决了dispatch group的并发问题。

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

关于ios - Swift - 如何获取 .Mov 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54114568/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com