【发布时间】:2021-08-20 07:23:44
【问题描述】:
已经有几天了,我正在努力解决这个问题。我正在从照片库中拍摄视频并使用它来创建缩略图。我的代码工作正常,我正在获取本地视频的视频数据和缩略图。
但是当我选择 iCloud 中的视频时,我得到了视频数据,但无法创建缩略图。let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)。
返回错误:The file couldn’t be opened because you don’t have permission to view it.
我在这里找到了类似的问题,例如this 一个,但解决方案是在功能下打开沙箱(我在 xcode 12 中没有找到)。也试过this 一个,我得到了同样的错误。 所有其他类似的问题都是针对应用程序沙箱之外的文件,但我的是来自照片库的视频。我不应该能够在我的应用程序中访问它吗?下面是我的代码。请帮忙。
let options = PHVideoRequestOptions()
options.isNetworkAccessAllowed = true
options.progressHandler = { (progress, error, stop, info) in
print("progress: \(progress)")
}
//phAsset is asset fetched from photo library
PHImageManager.default().requestAVAsset(forVideo: phAsset, options: options)
{(avAsset, mix, info) in
let myAsset = avAsset as? AVURLAsset
do {
if let url = myAsset?.url{
let videoData = try Data(contentsOf:url)
DispatchQueue.global(qos: .background).async {
let assetImgGenerate = AVAssetImageGenerator(asset: avAsset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTime(seconds: 0.0, preferredTimescale: 600)
do {
let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: img)
} catch {
//here prints : "The file couldn’t be opened because you don’t have permission to view it."
print(error.localizedDescription)
}
}
}
} catch {
print("exception catch at block - while uploading video")
}
}
【问题讨论】:
-
你怎么知道应用已经完成了剪辑的下载?
-
我正在打印进度。我可以看到它在控制台中完成。我也是在下载完成后才获得AVasset,对吗?
标签: ios swift icloud phphotolibrary