Use video thumbnails from API when possible

This commit is contained in:
Shadowfacts 2021-11-07 15:10:18 -05:00
parent 5a26739b78
commit 1ec85ca095
1 changed files with 20 additions and 9 deletions

View File

@ -184,15 +184,26 @@ class AttachmentView: UIImageView, GIFAnimatable {
} }
func loadVideo() { func loadVideo() {
let attachmentURL = self.attachment.url if let previewURL = self.attachment.previewURL {
// todo: use a single dispatch queue attachmentRequest = ImageCache.attachments.get(previewURL, completion: { [weak self] (data, _ )in
DispatchQueue.global(qos: .userInitiated).async { guard let self = self, let data = data else { return }
let asset = AVURLAsset(url: attachmentURL) DispatchQueue.main.async {
let generator = AVAssetImageGenerator(asset: asset) self.attachmentRequest = nil
generator.appliesPreferredTrackTransform = true self.source = .imageData(previewURL, data)
guard let image = try? generator.copyCGImage(at: .zero, actualTime: nil) else { return } self.displayImage()
self.source = .cgImage(attachmentURL, image) }
self.displayImage() })
} else {
let attachmentURL = self.attachment.url
// todo: use a single dispatch queue
DispatchQueue.global(qos: .userInitiated).async {
let asset = AVURLAsset(url: attachmentURL)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
guard let image = try? generator.copyCGImage(at: .zero, actualTime: nil) else { return }
self.source = .cgImage(attachmentURL, image)
self.displayImage()
}
} }
let playImageView = UIImageView(image: UIImage(systemName: "play.circle.fill")) let playImageView = UIImageView(image: UIImage(systemName: "play.circle.fill"))