diff --git a/lottie-ios/Classes/Private/LOTAnimationCache.m b/lottie-ios/Classes/Private/LOTAnimationCache.m index 9b5eaf58c3..fc9af68cdb 100644 --- a/lottie-ios/Classes/Private/LOTAnimationCache.m +++ b/lottie-ios/Classes/Private/LOTAnimationCache.m @@ -59,5 +59,9 @@ const NSInteger kLOTCacheSize = 50; [lruOrderArray_ removeAllObjects]; } +- (void)removeAnimationForKey:(NSString *)key { + [lruOrderArray_ removeObject:key]; + [animationsCache_ removeObjectForKey:key]; +} @end diff --git a/lottie-ios/Classes/Private/LOTAnimationView.m b/lottie-ios/Classes/Private/LOTAnimationView.m index 8e255d5dde..48ee918624 100644 --- a/lottie-ios/Classes/Private/LOTAnimationView.m +++ b/lottie-ios/Classes/Private/LOTAnimationView.m @@ -43,7 +43,9 @@ if (JSONObject && !error) { LOTComposition *laScene = [[LOTComposition alloc] initWithJSON:JSONObject withAssetBundle:bundle]; [[LOTAnimationCache sharedCache] addAnimation:laScene forKey:animationName]; - return [[LOTAnimationView alloc] initWithModel:laScene inBundle:bundle]; + LOTAnimationView *animationView = [[LOTAnimationView alloc] initWithModel:laScene inBundle:bundle]; + animationView.cacheKey = animationName; + return animationView; } NSLog(@"%s: Animation Not Found", __PRETTY_FUNCTION__); return [[LOTAnimationView alloc] initWithModel:nil inBundle:nil]; @@ -74,7 +76,9 @@ LOTComposition *laScene = [[LOTComposition alloc] initWithJSON:JSONObject withAssetBundle:[NSBundle mainBundle]]; laScene.rootDirectory = [filePath stringByDeletingLastPathComponent]; [[LOTAnimationCache sharedCache] addAnimation:laScene forKey:animationName]; - return [[LOTAnimationView alloc] initWithModel:laScene inBundle:[NSBundle mainBundle]]; + LOTAnimationView *animationView = [[LOTAnimationView alloc] initWithModel:laScene inBundle:[NSBundle mainBundle]]; + animationView.cacheKey = animationName; + return animationView; } NSLog(@"%s: Animation Not Found", __PRETTY_FUNCTION__); @@ -86,6 +90,7 @@ if (self) { LOTComposition *laScene = [[LOTAnimationCache sharedCache] animationForKey:url.absoluteString]; if (laScene) { + self.cacheKey = url.absoluteString; [self _initializeAnimationContainer]; [self _setupWithSceneModel:laScene]; } else { @@ -104,6 +109,7 @@ LOTComposition *laScene = [[LOTComposition alloc] initWithJSON:animationJSON withAssetBundle:[NSBundle mainBundle]]; dispatch_async(dispatch_get_main_queue(), ^(void){ [[LOTAnimationCache sharedCache] addAnimation:laScene forKey:url.absoluteString]; + self.cacheKey = url.absoluteString; [self _initializeAnimationContainer]; [self _setupWithSceneModel:laScene]; }); @@ -140,6 +146,7 @@ #endif - (void)_setupWithSceneModel:(LOTComposition *)model { + _cacheEnable = YES; _animationSpeed = 1; _sceneModel = model; [CATransaction begin]; @@ -230,7 +237,6 @@ - (void)setProgressWithFrame:(nonnull NSNumber *)currentFrame { [self _removeCurrentAnimationIfNecessary]; [self _callCompletionIfNecessary:NO]; - CGFloat duration = _sceneModel.endFrame.floatValue - _sceneModel.startFrame.floatValue; _animationProgress = currentFrame.floatValue / (_sceneModel.endFrame.floatValue - _sceneModel.startFrame.floatValue); [CATransaction begin]; [CATransaction setDisableActions:YES]; @@ -239,6 +245,18 @@ [CATransaction commit]; } +- (void)setCacheEnable:(BOOL)cacheEnable{ + _cacheEnable = cacheEnable; + if (!self.cacheKey) { + return; + } + if (cacheEnable) { + [[LOTAnimationCache sharedCache] addAnimation:_sceneModel forKey:self.cacheKey]; + }else { + [[LOTAnimationCache sharedCache] removeAnimationForKey:self.cacheKey]; + } +} + #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR - (void)addSubview:(nonnull LOTView *)view diff --git a/lottie-ios/Classes/Private/LOTAnimationView_Internal.h b/lottie-ios/Classes/Private/LOTAnimationView_Internal.h index 74e351aa50..1537fd6f08 100644 --- a/lottie-ios/Classes/Private/LOTAnimationView_Internal.h +++ b/lottie-ios/Classes/Private/LOTAnimationView_Internal.h @@ -18,5 +18,6 @@ typedef enum : NSUInteger { @property (nonatomic, readonly) LOTComposition * _Nonnull sceneModel; @property (nonatomic, copy, nullable) LOTAnimationCompletionBlock completionBlock; +@property (nonatomic, copy, nullable) NSString *cacheKey; @end diff --git a/lottie-ios/Classes/PublicHeaders/LOTAnimationCache.h b/lottie-ios/Classes/PublicHeaders/LOTAnimationCache.h index b0f5a054d1..c2fd02ebe4 100644 --- a/lottie-ios/Classes/PublicHeaders/LOTAnimationCache.h +++ b/lottie-ios/Classes/PublicHeaders/LOTAnimationCache.h @@ -8,15 +8,27 @@ #import +NS_ASSUME_NONNULL_BEGIN + @class LOTComposition; @interface LOTAnimationCache : NSObject +/// Global Cache + (instancetype)sharedCache; +/// Adds animation to the cache - (void)addAnimation:(LOTComposition *)animation forKey:(NSString *)key; -- (LOTComposition *)animationForKey:(NSString *)key; +/// Returns animation from cache. +- (LOTComposition * _Nullable)animationForKey:(NSString *)key; + +/// Removes a specific animation from the cache +- (void)removeAnimationForKey:(NSString *)key; + +/// Clears Everything from the Cache - (void)clearCache; @end + +NS_ASSUME_NONNULL_END diff --git a/lottie-ios/Classes/PublicHeaders/LOTAnimationView.h b/lottie-ios/Classes/PublicHeaders/LOTAnimationView.h index 2f0e751589..5a0c35cc99 100644 --- a/lottie-ios/Classes/PublicHeaders/LOTAnimationView.h +++ b/lottie-ios/Classes/PublicHeaders/LOTAnimationView.h @@ -54,6 +54,9 @@ typedef void (^LOTAnimationCompletionBlock)(BOOL animationFinished); /// Read only of the duration in seconds of the animation at speed of 1 @property (nonatomic, readonly) CGFloat animationDuration; +/// Enables or disables caching of the backing animation model. Defaults to YES +@property (nonatomic, assign) BOOL cacheEnable; + /* * Plays the animation from its current position to a specific progress. * The animation will start from its current position.