From 1ec85ca0956532b68ae386a735864cf40ec1a828 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 7 Nov 2021 15:10:18 -0500 Subject: [PATCH] Use video thumbnails from API when possible --- Tusker/Views/Attachments/AttachmentView.swift | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/Tusker/Views/Attachments/AttachmentView.swift b/Tusker/Views/Attachments/AttachmentView.swift index d23b0098b3..cb987c62d8 100644 --- a/Tusker/Views/Attachments/AttachmentView.swift +++ b/Tusker/Views/Attachments/AttachmentView.swift @@ -184,15 +184,26 @@ class AttachmentView: UIImageView, GIFAnimatable { } func loadVideo() { - 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() + if let previewURL = self.attachment.previewURL { + attachmentRequest = ImageCache.attachments.get(previewURL, completion: { [weak self] (data, _ )in + guard let self = self, let data = data else { return } + DispatchQueue.main.async { + self.attachmentRequest = nil + self.source = .imageData(previewURL, data) + 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"))