From 1304d5944f014b296e34b3bf680df0da46cd5fb6 Mon Sep 17 00:00:00 2001 From: cry Date: Mon, 14 Aug 2023 02:01:49 +0800 Subject: [PATCH 1/2] Fix can't display single frame gif --- Sources/Gifu/Classes/Animator.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/Gifu/Classes/Animator.swift b/Sources/Gifu/Classes/Animator.swift index e3d70ed..0ae9a91 100644 --- a/Sources/Gifu/Classes/Animator.swift +++ b/Sources/Gifu/Classes/Animator.swift @@ -131,6 +131,8 @@ public class Animator { func startAnimating() { if frameStore?.isAnimatable ?? false { displayLink.isPaused = false + } else { + delegate.animatorHasNewFrame() } } From 6b989c57695520b372c10da05bcc27ce23ec58e0 Mon Sep 17 00:00:00 2001 From: cry Date: Mon, 14 Aug 2023 02:59:03 +0800 Subject: [PATCH 2/2] Change to play single frame image method --- Sources/Gifu/Classes/Animator.swift | 30 ++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/Sources/Gifu/Classes/Animator.swift b/Sources/Gifu/Classes/Animator.swift index 0ae9a91..9dbfbb4 100644 --- a/Sources/Gifu/Classes/Animator.swift +++ b/Sources/Gifu/Classes/Animator.swift @@ -23,6 +23,9 @@ public class Animator { /// A delegate responsible for displaying the GIF frames. private weak var delegate: GIFAnimatable! + + /// Callback for when preparation is done + private var preparationBlock: (() -> Void)? = nil /// Callback for when all the loops of the animation are done (never called for infinite loops) private var animationBlock: (() -> Void)? = nil @@ -132,7 +135,22 @@ public class Animator { if frameStore?.isAnimatable ?? false { displayLink.isPaused = false } else { - delegate.animatorHasNewFrame() + let block = preparationBlock + preparationBlock = { [weak self] in + block?() + + self?.showSingleFrameImage() + } + + showSingleFrameImage() + } + } + + // Use when source not animatable. + private func showSingleFrameImage() { + DispatchQueue.main.async { [self] in + var container = delegate as? ImageContainer + container?.image = frameStore?.currentFrameImage } } @@ -151,13 +169,16 @@ public class Animator { /// - parameter animationBlock: Callback for when all the loops of the animation are done (never called for infinite loops) /// - parameter loopBlock: Callback for when a loop is done (at the end of each loop) func animate(withGIFNamed imageName: String, size: CGSize, contentMode: UIView.ContentMode, loopCount: Int = 0, preparationBlock: (() -> Void)? = nil, animationBlock: (() -> Void)? = nil, loopBlock: (() -> Void)? = nil) { + self.preparationBlock = preparationBlock self.animationBlock = animationBlock self.loopBlock = loopBlock prepareForAnimation(withGIFNamed: imageName, size: size, contentMode: contentMode, loopCount: loopCount, - completionHandler: preparationBlock) + completionHandler: { [weak self] in + self?.preparationBlock?() + }) startAnimating() } @@ -171,13 +192,16 @@ public class Animator { /// - parameter animationBlock: Callback for when all the loops of the animation are done (never called for infinite loops) /// - parameter loopBlock: Callback for when a loop is done (at the end of each loop) func animate(withGIFData imageData: Data, size: CGSize, contentMode: UIView.ContentMode, loopCount: Int = 0, preparationBlock: (() -> Void)? = nil, animationBlock: (() -> Void)? = nil, loopBlock: (() -> Void)? = nil) { + self.preparationBlock = preparationBlock self.animationBlock = animationBlock self.loopBlock = loopBlock prepareForAnimation(withGIFData: imageData, size: size, contentMode: contentMode, loopCount: loopCount, - completionHandler: preparationBlock) + completionHandler: { [weak self] in + self?.preparationBlock?() + }) startAnimating() }