Browse Source

implement video thumbnails #117

cyberta 5 years ago
parent
commit
c95d49394f
2 changed files with 9 additions and 10 deletions
  1. 4 3
      deltachat-ios/DC/Wrapper.swift
  2. 5 7
      deltachat-ios/Helper/Utils.swift

+ 4 - 3
deltachat-ios/DC/Wrapper.swift

@@ -612,12 +612,13 @@ class DcMsg: MessageType {
     }()
 
     internal func createVideoMessage(text: String) -> MessageKind {
+        let thumbnail = Utils.generateThumbnailFromVideo(url: fileURL)
         if text.isEmpty {
-                       return MessageKind.video(Media(url: fileURL))
-                   }
+            return MessageKind.video(Media(url: fileURL, image: thumbnail))
+        }
         let attributedString = NSAttributedString(string: text, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16.0),
                                                                              NSAttributedString.Key.foregroundColor: DcColors.defaultTextColor])
-                   return MessageKind.videoText(Media(url: fileURL, text: attributedString))
+        return MessageKind.videoText(Media(url: fileURL, image: thumbnail, text: attributedString))
     }
 
     internal func createImageMessage(text: String) -> MessageKind {

+ 5 - 7
deltachat-ios/Helper/Utils.swift

@@ -159,20 +159,18 @@ struct Utils {
         return url.absoluteString.hasSuffix("wav")
     }
 
-    static func generateThumbnailFromVideo(url: URL) -> UIImage? {
+    static func generateThumbnailFromVideo(url: URL?) -> UIImage? {
+        guard let url = url else {
+            return nil
+        }
         do {
             let asset = AVURLAsset(url: url)
             let imageGenerator = AVAssetImageGenerator(asset: asset)
             imageGenerator.appliesPreferredTrackTransform = true
-            // Select the right one based on which version you are using
-            // Swift 4.2
-            //let cgImage = try imageGenerator.copyCGImage(at: .zero, actualTime: nil)
-            // Swift 4.0
-            let cgImage = try imageGenerator.copyCGImage(at: CMTime.zero, actualTime: nil)
+            let cgImage = try imageGenerator.copyCGImage(at: .zero, actualTime: nil)
             return UIImage(cgImage: cgImage)
         } catch {
             print(error.localizedDescription)
-
             return nil
         }
     }