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 f3a50ff..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,8 @@ 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..4f9b428 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 mainRunLoopMode; - (void)startAnimation; - (void)startAnimationWithRange:(NSRange)range reverse:(BOOL)reverse; diff --git a/Source/SVGAPlayer.m b/Source/SVGAPlayer.m index 61aa19e..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:NSRunLoopCommonModes]; + [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:NSRunLoopCommonModes]; + [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:self.mainRunLoopMode]; } } @@ -523,4 +523,11 @@ return _dynamicDrawings; } +- (NSRunLoopMode)mainRunLoopMode { + if (!_mainRunLoopMode) { + _mainRunLoopMode = NSRunLoopCommonModes; + } + return _mainRunLoopMode; +} + @end diff --git a/Source/SVGAVideoEntity.m b/Source/SVGAVideoEntity.m index 66ab2ec..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,19 +211,26 @@ static NSMapTable * weakCache; } + (SVGAVideoEntity *)readCache:(NSString *)cacheKey { + dispatch_semaphore_wait(videoSemaphore, DISPATCH_TIME_FOREVER); SVGAVideoEntity * object = [videoCache objectForKey:cacheKey]; if (!object) { object = [weakCache objectForKey:cacheKey]; } - return object; + dispatch_semaphore_signal(videoSemaphore); + + return object; } - (void)saveCache:(NSString *)cacheKey { + dispatch_semaphore_wait(videoSemaphore, DISPATCH_TIME_FOREVER); [videoCache setObject:self forKey:cacheKey]; + dispatch_semaphore_signal(videoSemaphore); } - (void)saveWeakCache:(NSString *)cacheKey { + dispatch_semaphore_wait(videoSemaphore, DISPATCH_TIME_FOREVER); [weakCache setObject:self forKey:cacheKey]; + dispatch_semaphore_signal(videoSemaphore); } @end