I wonder why the contentsOf returns nil for URL from AVURLAsset. After picking from the custom library with Photos framework, I tried to request the asset from PHAsset like the following:
PHCachingImageManager().requestAVAsset(forVideo: asset, options: nil) { (avAsset, _, _) in
DispatchQueue.main.async {
guard let asset = avAsset as? AVURLAsset else {
return
}
print(asset.url) // file:///var/mobile/Media/DCIM/101APPLE/IMG_1513.MP4
}
}
The video with the URL above can be displayed normally with AVPlayer. But when I try to get the data associated with the url using:
do {
let videoData = try Data(contentsOf: mediaURL!)
} catch (let error){
print(error.localizedDescription ?? "") // "The file “IMG_1490.MP4” couldn’t be opened because you don’t have permission to view it."
}