From b9351df49c66f86c0742d195547f0ac75705b353 Mon Sep 17 00:00:00 2001 From: onevcat Date: Tue, 31 Jan 2017 16:44:28 +0900 Subject: [PATCH] Remove layer content when image set to nil --- APNGKit/APNGImageView.swift | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/APNGKit/APNGImageView.swift b/APNGKit/APNGImageView.swift index 4101e51..08e07e5 100644 --- a/APNGKit/APNGImageView.swift +++ b/APNGKit/APNGImageView.swift @@ -44,19 +44,23 @@ open class APNGImageView: UIView { let animating = isAnimating stopAnimating() - image?.reset() + guard let image = image else { + updateContents(nil) + return + } - if let frame = image?.next(currentIndex: currentFrameIndex) { - currentFrameDuration = frame.duration - updateContents(frame.image) - - if animating { - startAnimating() - } - - if autoStartAnimation { - startAnimating() - } + image.reset() + + let frame = image.next(currentIndex: currentFrameIndex) + currentFrameDuration = frame.duration + updateContents(frame.image) + + if animating { + startAnimating() + } + + if autoStartAnimation { + startAnimating() } } } @@ -212,6 +216,9 @@ open class APNGImageView: UIView { delegate?.apngImageView?(self, didFinishPlaybackForRepeatedCount: repeated) + // If user set image to `nil`, do not render anymore. + guard let _ = self.image else { return } + currentFrameIndex = 0 repeated = repeated + 1 @@ -230,6 +237,7 @@ open class APNGImageView: UIView { } currentPassedDuration = currentPassedDuration - currentFrameDuration + let frame = image.next(currentIndex: currentFrameIndex) currentFrameDuration = frame.duration updateContents(frame.image)