我正在使用从Firebase链接获取视频的应用程序。为了能够显示和播放视频,我正在使用此扩展程序:
func generateThumbnailForVideoAtURL(filePathLocal: NSString) -> UIImage? {
let vidURL = NSURL(fileURLWithPath:filePathLocal as String)
let asset = AVURLAsset(url: vidURL as URL)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
let timestamp = CMTime(seconds: 1, preferredTimescale: 60)
do {
let imageRef = try generator.copyCGImage(at: timestamp, actualTime: nil)
return UIImage(cgImage: imageRef)
} catch let error as NSError {
print("Image generation failed with error \(error)")
return nil
}
}
然后在cellAtIndexPath
中像这样使用它:
if posts[indexPath.row].videoUrl != nil {
cell.videoView.generateThumbnailForVideoAtURL(filePathLocal: self.posts[indexPath.row].videoUrl as! NSString)
print("VIDEO IS: \(posts[indexPath.row].videoUrl)")
}
我收到此错误:
图像生成失败,错误错误域= NSURLErrorDomain代码= -1102“您无权访问所请求的资源。” UserInfo = {NSLocalizedDescription =您无权访问所请求的资源。NSUnderlyingError = 0x1c4046ff0 {Error Domain = NSOSStatusErrorDomain Code = -12660“(null)”}}]
似乎是一个UserInfo描述,但是我在plist中找不到要添加权限的任何内容。这可能对其他人来说听起来很愚蠢,因为这是我现在的感觉,因为我找不到使它起作用的方法。请帮助!
您应该使用此让vidURL = URL(string:filePathLocal as String)
而不是让vidURL = NSURL(fileURLWithPath:filePathLocal as String)