Răsfoiți Sursa

make a copy of current frame source when creating `AnimatedImageView`

Yang Chao 1 an în urmă
părinte
comite
982f174dd9

+ 18 - 0
Sources/Image/GIFAnimatedImage.swift

@@ -148,6 +148,13 @@ public protocol ImageFrameSource {
     
     /// Retrieves the duration at a specific index. If the index is invalid, implementors should return `0.0`.
     func duration(at index: Int) -> TimeInterval
+    
+    /// Creates a copy of the current `ImageFrameSource` instance.
+    ///
+    /// - Returns: A new instance of the same type as `self` with identical properties.
+    ///            If not overridden by conforming types, this default implementation
+    ///            simply returns `self`, which may not create an actual copy if the type is a reference type.
+    func copy() -> Self
 }
 
 public extension ImageFrameSource {
@@ -156,6 +163,10 @@ public extension ImageFrameSource {
     func frame(at index: Int) -> CGImage? {
         return frame(at: index, maxSize: nil)
     }
+    
+    func copy() -> Self {
+        return self
+    }
 }
 
 struct CGImageFrameSource: ImageFrameSource {
@@ -183,5 +194,12 @@ struct CGImageFrameSource: ImageFrameSource {
     func duration(at index: Int) -> TimeInterval {
         return GIFAnimatedImage.getFrameDuration(from: imageSource, at: index)
     }
+    
+    func copy() -> Self {
+        guard let data = data, let source = CGImageSourceCreateWithData(data as CFData, options as CFDictionary?) else {
+            return self
+        }
+        return CGImageFrameSource(data: data, imageSource: source, options: options)
+    }
 }
 

+ 1 - 1
Sources/Views/AnimatedImageView.swift

@@ -672,7 +672,7 @@ extension AnimatedImageView {
              framePreloadCount count: Int,
              repeatCount: RepeatCount,
              preloadQueue: DispatchQueue) {
-            self.frameSource = source
+            self.frameSource = source.copy()
             self.contentMode = mode
             self.size = size
             self.imageSize = imageSize