From 805ce0865ed7a016f6de4abef563d691f4dc9d26 Mon Sep 17 00:00:00 2001 From: JerryFans Date: Thu, 7 Sep 2023 19:00:59 +0800 Subject: [PATCH 1/6] feature: add a switch to ignore internal image cache or not , in image provider protocol, default is false.(some scene some one want to custom the image cache in custom image provider such as replace some image in a custom image provider, if internal image cache enable, the replace event will fail.) --- .../LayerContainers/Utility/CachedImageProvider.swift | 2 +- .../Public/ImageProvider/AnimationImageProvider.swift | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift b/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift index d2fa02cd97..69d6182c1f 100644 --- a/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift +++ b/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift @@ -21,7 +21,7 @@ private final class CachedImageProvider: AnimationImageProvider { // MARK: Public public func imageForAsset(asset: ImageAsset) -> CGImage? { - if let image = imageCache.object(forKey: asset.id as NSString) { + if imageProvider.ignoreInternalImageCache == false, let image = imageCache.object(forKey: asset.id as NSString) { return image } if let image = imageProvider.imageForAsset(asset: asset) { diff --git a/Sources/Public/ImageProvider/AnimationImageProvider.swift b/Sources/Public/ImageProvider/AnimationImageProvider.swift index f9358a2f7b..41c576776b 100644 --- a/Sources/Public/ImageProvider/AnimationImageProvider.swift +++ b/Sources/Public/ImageProvider/AnimationImageProvider.swift @@ -17,5 +17,15 @@ import Foundation /// Additionally custom Image Providers can be made to load images from a URL, /// or to Cache images. public protocol AnimationImageProvider { + + var ignoreInternalImageCache: Bool { get set } + func imageForAsset(asset: ImageAsset) -> CGImage? } + +public extension AnimationImageProvider { + var ignoreInternalImageCache: Bool { + get { return false } + set { } + } +} From efddb6fd0577e27367ec1f0eb9853bf6bb977a62 Mon Sep 17 00:00:00 2001 From: JerryFans Date: Fri, 8 Sep 2023 01:30:02 +0800 Subject: [PATCH 2/6] fix property describe and format code. --- .../Utility/CachedImageProvider.swift | 2 +- .../AnimationImageProvider.swift | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift b/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift index 69d6182c1f..253dfd2495 100644 --- a/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift +++ b/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift @@ -21,7 +21,7 @@ private final class CachedImageProvider: AnimationImageProvider { // MARK: Public public func imageForAsset(asset: ImageAsset) -> CGImage? { - if imageProvider.ignoreInternalImageCache == false, let image = imageCache.object(forKey: asset.id as NSString) { + if imageProvider.cacheEligible, let image = imageCache.object(forKey: asset.id as NSString) { return image } if let image = imageProvider.imageForAsset(asset: asset) { diff --git a/Sources/Public/ImageProvider/AnimationImageProvider.swift b/Sources/Public/ImageProvider/AnimationImageProvider.swift index 41c576776b..f46c785830 100644 --- a/Sources/Public/ImageProvider/AnimationImageProvider.swift +++ b/Sources/Public/ImageProvider/AnimationImageProvider.swift @@ -8,6 +8,8 @@ import CoreGraphics import Foundation +// MARK: - AnimationImageProvider + /// Image provider is a protocol that is used to supply images to `LottieAnimationView`. /// /// Some animations require a reference to an image. The image provider loads and @@ -17,15 +19,17 @@ import Foundation /// Additionally custom Image Providers can be made to load images from a URL, /// or to Cache images. public protocol AnimationImageProvider { - - var ignoreInternalImageCache: Bool { get set } - + + /// Whether or not the resulting image of this image provider can be cached by Lottie. Defaults to true. + /// If true, Lottie may internally cache the result of `imageForAsset` + var cacheEligible: Bool { get set } + func imageForAsset(asset: ImageAsset) -> CGImage? } -public extension AnimationImageProvider { - var ignoreInternalImageCache: Bool { - get { return false } - set { } - } +extension AnimationImageProvider { + public var cacheEligible: Bool { + get { true } + set { } + } } From 03731aeddfdf6846b7ddae19b63ba0ef723d62bd Mon Sep 17 00:00:00 2001 From: Cal Stephens Date: Thu, 7 Sep 2023 10:32:49 -0700 Subject: [PATCH 3/6] Apply suggestions from code review --- Sources/Public/ImageProvider/AnimationImageProvider.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/Public/ImageProvider/AnimationImageProvider.swift b/Sources/Public/ImageProvider/AnimationImageProvider.swift index f46c785830..29ac0c9cdc 100644 --- a/Sources/Public/ImageProvider/AnimationImageProvider.swift +++ b/Sources/Public/ImageProvider/AnimationImageProvider.swift @@ -24,6 +24,7 @@ public protocol AnimationImageProvider { /// If true, Lottie may internally cache the result of `imageForAsset` var cacheEligible: Bool { get set } + /// The image to display for the given `ImageAsset` defined in the `LottieAnimation` JSON file. func imageForAsset(asset: ImageAsset) -> CGImage? } From c4010028a105ea51bdc0393a3f97370feb7e760f Mon Sep 17 00:00:00 2001 From: Cal Stephens Date: Thu, 7 Sep 2023 10:33:29 -0700 Subject: [PATCH 4/6] Apply suggestions from code review --- Sources/Public/ImageProvider/AnimationImageProvider.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Sources/Public/ImageProvider/AnimationImageProvider.swift b/Sources/Public/ImageProvider/AnimationImageProvider.swift index 29ac0c9cdc..e87083206a 100644 --- a/Sources/Public/ImageProvider/AnimationImageProvider.swift +++ b/Sources/Public/ImageProvider/AnimationImageProvider.swift @@ -22,7 +22,7 @@ public protocol AnimationImageProvider { /// Whether or not the resulting image of this image provider can be cached by Lottie. Defaults to true. /// If true, Lottie may internally cache the result of `imageForAsset` - var cacheEligible: Bool { get set } + var cacheEligible: Bool { get } /// The image to display for the given `ImageAsset` defined in the `LottieAnimation` JSON file. func imageForAsset(asset: ImageAsset) -> CGImage? @@ -30,7 +30,6 @@ public protocol AnimationImageProvider { extension AnimationImageProvider { public var cacheEligible: Bool { - get { true } - set { } + true } } From 93f5aaa3155637ea980fa7d33c79305d4020586a Mon Sep 17 00:00:00 2001 From: JerryFans Date: Fri, 8 Sep 2023 01:37:23 +0800 Subject: [PATCH 5/6] fix cacheEligible only get need. --- Sources/Public/ImageProvider/AnimationImageProvider.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Sources/Public/ImageProvider/AnimationImageProvider.swift b/Sources/Public/ImageProvider/AnimationImageProvider.swift index f46c785830..97852aba01 100644 --- a/Sources/Public/ImageProvider/AnimationImageProvider.swift +++ b/Sources/Public/ImageProvider/AnimationImageProvider.swift @@ -22,14 +22,13 @@ public protocol AnimationImageProvider { /// Whether or not the resulting image of this image provider can be cached by Lottie. Defaults to true. /// If true, Lottie may internally cache the result of `imageForAsset` - var cacheEligible: Bool { get set } + var cacheEligible: Bool { get } func imageForAsset(asset: ImageAsset) -> CGImage? } extension AnimationImageProvider { public var cacheEligible: Bool { - get { true } - set { } + true } } From 9155de3ec106801a6c4e691f6955f7818f51a2d8 Mon Sep 17 00:00:00 2001 From: JerryFans Date: Fri, 8 Sep 2023 01:59:12 +0800 Subject: [PATCH 6/6] if not cacheEligible, return custom image provider. --- .../LayerContainers/Utility/CachedImageProvider.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift b/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift index 253dfd2495..c74355672d 100644 --- a/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift +++ b/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift @@ -21,7 +21,7 @@ private final class CachedImageProvider: AnimationImageProvider { // MARK: Public public func imageForAsset(asset: ImageAsset) -> CGImage? { - if imageProvider.cacheEligible, let image = imageCache.object(forKey: asset.id as NSString) { + if let image = imageCache.object(forKey: asset.id as NSString) { return image } if let image = imageProvider.imageForAsset(asset: asset) { @@ -42,6 +42,7 @@ extension AnimationImageProvider { /// It wraps the current provider as image loader, and uses `NSCache` to cache the images for resue. /// The cache will be reset when the `animation` is reset. var cachedImageProvider: AnimationImageProvider { - CachedImageProvider(imageProvider: self) + guard cacheEligible else { return self } + return CachedImageProvider(imageProvider: self) } }