From fa0059d8bf50ad346a2e3ef4417b6d35a5c49377 Mon Sep 17 00:00:00 2001 From: yangqingren <564008993@qq.com> Date: Fri, 31 Jul 2020 17:56:07 +0800 Subject: [PATCH 1/4] feat: added to use in mian loop modes --- SVGAPlayer/ViewController.m | 1 + Source/SVGAPlayer.h | 1 + Source/SVGAPlayer.m | 11 +++++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/SVGAPlayer/ViewController.m b/SVGAPlayer/ViewController.m index f3a50ff..237d8bd 100644 --- a/SVGAPlayer/ViewController.m +++ b/SVGAPlayer/ViewController.m @@ -66,6 +66,7 @@ static SVGAParser *parser; NSParagraphStyleAttributeName: para, }]; [self.aPlayer setAttributedText:str forKey:@"banner"]; +// self.aPlayer.mianRunLoopMode = NSDefaultRunLoopMode; [self.aPlayer startAnimation]; } } failureBlock:nil]; diff --git a/Source/SVGAPlayer.h b/Source/SVGAPlayer.h index 9d22557..771669b 100644 --- a/Source/SVGAPlayer.h +++ b/Source/SVGAPlayer.h @@ -28,6 +28,7 @@ typedef void(^SVGAPlayerDynamicDrawingBlock)(CALayer *contentLayer, NSInteger fr @property (nonatomic, assign) IBInspectable int loops; @property (nonatomic, assign) IBInspectable BOOL clearsAfterStop; @property (nonatomic, copy) NSString *fillMode; +@property (nonatomic, copy) NSRunLoopMode mianRunLoopMode; - (void)startAnimation; - (void)startAnimationWithRange:(NSRange)range reverse:(BOOL)reverse; diff --git a/Source/SVGAPlayer.m b/Source/SVGAPlayer.m index 61aa19e..991cafd 100644 --- a/Source/SVGAPlayer.m +++ b/Source/SVGAPlayer.m @@ -84,7 +84,7 @@ } self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)]; self.displayLink.frameInterval = 60 / self.videoItem.FPS; - [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; + [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:self.mianRunLoopMode]; self.forwardAnimating = !self.reversing; } @@ -156,7 +156,7 @@ } self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)]; self.displayLink.frameInterval = 60 / self.videoItem.FPS; - [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; + [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:self.mianRunLoopMode]; } } @@ -523,4 +523,11 @@ return _dynamicDrawings; } +- (NSRunLoopMode)mianRunLoopMode { + if (!_mianRunLoopMode) { + _mianRunLoopMode = NSRunLoopCommonModes; + } + return _mianRunLoopMode; +} + @end From 523071052b1ecb387e1e650b4f28474cf610b045 Mon Sep 17 00:00:00 2001 From: yangqingren <564008993@qq.com> Date: Fri, 31 Jul 2020 18:44:16 +0800 Subject: [PATCH 2/4] fix: cache added synchronized --- SVGAPlayer.podspec | 2 +- SVGAPlayer/ViewController.m | 2 ++ Source/SVGAVideoEntity.m | 20 ++++++++++++++------ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/SVGAPlayer.podspec b/SVGAPlayer.podspec index f10295d..c5cd46f 100644 --- a/SVGAPlayer.podspec +++ b/SVGAPlayer.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "SVGAPlayer" - s.version = "2.5.4" + s.version = "2.5.5" s.summary = "SVGAPlayer 是一个高性能的动画播放器" s.description = <<-DESC SVGA 是一种全新的动画格式,由 YY UED 团队主导开发; diff --git a/SVGAPlayer/ViewController.m b/SVGAPlayer/ViewController.m index 237d8bd..ab84ce7 100644 --- a/SVGAPlayer/ViewController.m +++ b/SVGAPlayer/ViewController.m @@ -50,6 +50,7 @@ static SVGAParser *parser; @"https://github.com/yyued/SVGA-Samples/blob/master/rose.svga?raw=true", ]; [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; +// parser.enabledMemoryCache = YES; [parser parseWithURL:[NSURL URLWithString:items[arc4random() % items.count]] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; @@ -66,6 +67,7 @@ static SVGAParser *parser; NSParagraphStyleAttributeName: para, }]; [self.aPlayer setAttributedText:str forKey:@"banner"]; + // self.aPlayer.mianRunLoopMode = NSDefaultRunLoopMode; [self.aPlayer startAnimation]; } diff --git a/Source/SVGAVideoEntity.m b/Source/SVGAVideoEntity.m index 66ab2ec..44eeb8c 100644 --- a/Source/SVGAVideoEntity.m +++ b/Source/SVGAVideoEntity.m @@ -209,19 +209,27 @@ static NSMapTable * weakCache; } + (SVGAVideoEntity *)readCache:(NSString *)cacheKey { - SVGAVideoEntity * object = [videoCache objectForKey:cacheKey]; - if (!object) { - object = [weakCache objectForKey:cacheKey]; + @synchronized (videoCache) { + SVGAVideoEntity * object = [videoCache objectForKey:cacheKey]; + if (!object) { + @synchronized (weakCache) { + object = [weakCache objectForKey:cacheKey]; + } + } + return object; } - return object; } - (void)saveCache:(NSString *)cacheKey { - [videoCache setObject:self forKey:cacheKey]; + @synchronized (videoCache) { + [videoCache setObject:self forKey:cacheKey]; + } } - (void)saveWeakCache:(NSString *)cacheKey { - [weakCache setObject:self forKey:cacheKey]; + @synchronized (weakCache) { + [weakCache setObject:self forKey:cacheKey]; + } } @end From c9a9cacdc7595f0937df5f5f75709be23cd003fd Mon Sep 17 00:00:00 2001 From: yangqingren <564008993@qq.com> Date: Mon, 12 Oct 2020 13:03:53 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=E5=B0=86=20synchronize=20=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=20semaphore=20=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SVGAVideoEntity.m | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Source/SVGAVideoEntity.m b/Source/SVGAVideoEntity.m index 44eeb8c..cd63400 100644 --- a/Source/SVGAVideoEntity.m +++ b/Source/SVGAVideoEntity.m @@ -32,6 +32,7 @@ static NSCache *videoCache; static NSMapTable * weakCache; +static dispatch_semaphore_t videoSemaphore; + (void)load { static dispatch_once_t onceToken; @@ -40,6 +41,7 @@ static NSMapTable * weakCache; weakCache = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory capacity:64]; + videoSemaphore = dispatch_semaphore_create(1); }); } @@ -209,27 +211,26 @@ static NSMapTable * weakCache; } + (SVGAVideoEntity *)readCache:(NSString *)cacheKey { - @synchronized (videoCache) { - SVGAVideoEntity * object = [videoCache objectForKey:cacheKey]; - if (!object) { - @synchronized (weakCache) { - object = [weakCache objectForKey:cacheKey]; - } - } - return object; + dispatch_semaphore_wait(videoSemaphore, DISPATCH_TIME_FOREVER); + SVGAVideoEntity * object = [videoCache objectForKey:cacheKey]; + if (!object) { + object = [weakCache objectForKey:cacheKey]; } + dispatch_semaphore_signal(videoSemaphore); + + return object; } - (void)saveCache:(NSString *)cacheKey { - @synchronized (videoCache) { - [videoCache setObject:self forKey:cacheKey]; - } + dispatch_semaphore_wait(videoSemaphore, DISPATCH_TIME_FOREVER); + [videoCache setObject:self forKey:cacheKey]; + dispatch_semaphore_signal(videoSemaphore); } - (void)saveWeakCache:(NSString *)cacheKey { - @synchronized (weakCache) { - [weakCache setObject:self forKey:cacheKey]; - } + dispatch_semaphore_wait(videoSemaphore, DISPATCH_TIME_FOREVER); + [weakCache setObject:self forKey:cacheKey]; + dispatch_semaphore_signal(videoSemaphore); } @end From 2e3f3c8c41e569573adfa45e695048f42662b6f0 Mon Sep 17 00:00:00 2001 From: yangqingren <564008993@qq.com> Date: Mon, 12 Oct 2020 18:14:28 +0800 Subject: [PATCH 4/4] fix: main runloop 'main' --- Source/SVGAPlayer.h | 2 +- Source/SVGAPlayer.m | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Source/SVGAPlayer.h b/Source/SVGAPlayer.h index 771669b..4f9b428 100644 --- a/Source/SVGAPlayer.h +++ b/Source/SVGAPlayer.h @@ -28,7 +28,7 @@ typedef void(^SVGAPlayerDynamicDrawingBlock)(CALayer *contentLayer, NSInteger fr @property (nonatomic, assign) IBInspectable int loops; @property (nonatomic, assign) IBInspectable BOOL clearsAfterStop; @property (nonatomic, copy) NSString *fillMode; -@property (nonatomic, copy) NSRunLoopMode mianRunLoopMode; +@property (nonatomic, copy) NSRunLoopMode mainRunLoopMode; - (void)startAnimation; - (void)startAnimationWithRange:(NSRange)range reverse:(BOOL)reverse; diff --git a/Source/SVGAPlayer.m b/Source/SVGAPlayer.m index 991cafd..18ccc2d 100644 --- a/Source/SVGAPlayer.m +++ b/Source/SVGAPlayer.m @@ -84,7 +84,7 @@ } self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)]; self.displayLink.frameInterval = 60 / self.videoItem.FPS; - [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:self.mianRunLoopMode]; + [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:self.mainRunLoopMode]; self.forwardAnimating = !self.reversing; } @@ -156,7 +156,7 @@ } self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)]; self.displayLink.frameInterval = 60 / self.videoItem.FPS; - [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:self.mianRunLoopMode]; + [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:self.mainRunLoopMode]; } } @@ -523,11 +523,11 @@ return _dynamicDrawings; } -- (NSRunLoopMode)mianRunLoopMode { - if (!_mianRunLoopMode) { - _mianRunLoopMode = NSRunLoopCommonModes; +- (NSRunLoopMode)mainRunLoopMode { + if (!_mainRunLoopMode) { + _mainRunLoopMode = NSRunLoopCommonModes; } - return _mianRunLoopMode; + return _mainRunLoopMode; } @end