Răsfoiți Sursa

Supports background gif decoding

xspyhack 6 ani în urmă
părinte
comite
7c660835f9
1 a modificat fișierele cu 10 adăugiri și 1 ștergeri
  1. 10 1
      Sources/Views/AnimatedImageView.swift

+ 10 - 1
Sources/Views/AnimatedImageView.swift

@@ -120,6 +120,10 @@ open class AnimatedImageView: UIImageView {
     /// Default is `true`.
     public var needsPrescaling = true
 
+    /// Decode the GIF frames in background thread before using. It will decode frames data and do a off-screen
+    /// rendering to extract pixel information in background. This can reduce the main thread CPU usage.
+    public var backgroundDecode = true
+
     /// The animation timer's run loop mode. Default is `RunLoop.Mode.common`.
     /// Set this property to `RunLoop.Mode.default` will make the animation pause during UIScrollView scrolling.
     public var runLoopMode = KFRunLoopModeCommon {
@@ -251,6 +255,7 @@ open class AnimatedImageView: UIImageView {
                 preloadQueue: preloadQueue)
             animator.delegate = self
             animator.needsPrescaling = needsPrescaling
+            animator.backgroundDecode = backgroundDecode
             animator.prepareFramesAsynchronously()
             self.animator = animator
         }
@@ -361,6 +366,9 @@ extension AnimatedImageView {
         var isFinished: Bool = false
 
         var needsPrescaling = true
+
+        var backgroundDecode = true
+
         weak var delegate: AnimatorDelegate?
 
         // Total duration of one animation loop
@@ -501,7 +509,8 @@ extension AnimatedImageView {
                 return nil
             }
 
-            return Image(cgImage: cgImage)
+            let image = Image(cgImage: cgImage)
+            return backgroundDecode ? image.kf.decoded : image
         }
         
         private func updatePreloadedFrames() {