Reputation: 73
it has been a couple of days, I am struggling with this issue. I am taking a video from photo library and using it to create a thumbnail image. My code is working properly and I am getting the video data and thumbnail for local videos.
But when I select a video that is in iCloud, I am getting the video data but can't create thumbnail.
let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
.
returns error : The file couldn’t be opened because you don’t have permission to view it.
I have found similar questions here, like this one, but the solution was for it is to turn of sand box under capabilities(Which I didn't find in my xcode 12). Tried this one too, I got same error. All other similar questions are for files outside app sandbox, but mine is a video from photo library. Shouldn't I be able to access it within my app?. Below is my code. please help.
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")
}
}
Upvotes: 1
Views: 675
Reputation: 582
I've been seeing this same error on iOS 14 when simply trying to play a video from iCloud in an AVPlayer
. It seems like a bug in the Photos framework and the only "solution" that has worked for me has been to request the highest quality video.
options.deliveryMode = .highQualityFormat
See the comment from @SwiftRabbit on Request AVAsset using iCloud PHAsset returns an AVAsset with no VideoTracks
Upvotes: 1