From e0d01e262e5d4af33b231695c36d48af3cdbc8f3 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sat, 13 Jul 2019 18:32:07 +0800 Subject: [PATCH 1/3] Make animated image can resume from next frame after switch back to specified runloop mode --- .../AnimatedImages/PINAnimatedImageView.m | 18 ++++++---------- Source/Classes/PINDisplayLink.h | 1 + Source/Classes/PINDisplayLink.m | 21 ++++++++++++++++--- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Source/Classes/AnimatedImages/PINAnimatedImageView.m b/Source/Classes/AnimatedImages/PINAnimatedImageView.m index 53823bdf..40d955c5 100644 --- a/Source/Classes/AnimatedImages/PINAnimatedImageView.m +++ b/Source/Classes/AnimatedImages/PINAnimatedImageView.m @@ -22,8 +22,6 @@ @interface PINAnimatedImageView () @property (nonatomic, assign) CGImageRef frameImage; @property (nonatomic, strong) PINDisplayLink *displayLink; -@property (nonatomic, assign) CFTimeInterval lastDisplayLinkFire; - @end @implementation PINAnimatedImageView @@ -223,7 +221,6 @@ - (void)stopAnimating PINAssertMain(); _displayLink.paused = YES; - _lastDisplayLinkFire = 0; [_animatedImage clearAnimatedImageCache]; } @@ -336,14 +333,11 @@ - (void)updateAnimationForPossibleVisibility - (void)displayLinkFired:(PINDisplayLink *)displayLink { PINAssertMain(); - CFTimeInterval timeBetweenLastFire; - if (_lastDisplayLinkFire == 0) { - timeBetweenLastFire = 0; - } else { - timeBetweenLastFire = CACurrentMediaTime() - self.lastDisplayLinkFire; - } - - self.lastDisplayLinkFire = CACurrentMediaTime(); +#if PIN_TARGET_MAC + CFTimeInterval timeBetweenLastFire = displayLink.duration; +#else + CFTimeInterval timeBetweenLastFire = displayLink.duration * displayLink.frameInterval; +#endif _playHead += timeBetweenLastFire; @@ -367,7 +361,7 @@ - (void)displayLinkFired:(PINDisplayLink *)displayLink if (frameImage == nil) { //Pause the display link until we get a file ready notification displayLink.paused = YES; - self.lastDisplayLinkFire = 0; + _playHead = MAX(_playHead - timeBetweenLastFire, 0); } else { if (_frameImage) { CGImageRelease(_frameImage); diff --git a/Source/Classes/PINDisplayLink.h b/Source/Classes/PINDisplayLink.h index 0b43e7bf..06269208 100644 --- a/Source/Classes/PINDisplayLink.h +++ b/Source/Classes/PINDisplayLink.h @@ -20,6 +20,7 @@ @property(getter=isPaused, nonatomic) BOOL paused; @property(nonatomic) NSInteger frameInterval; +@property(nonatomic, readonly) CFTimeInterval duration; @end #endif diff --git a/Source/Classes/PINDisplayLink.m b/Source/Classes/PINDisplayLink.m index 73124704..1c838c09 100644 --- a/Source/Classes/PINDisplayLink.m +++ b/Source/Classes/PINDisplayLink.m @@ -16,7 +16,7 @@ @interface PINDisplayLink () @property (nonatomic, readonly) NSRunLoop *runloop; @property (nonatomic, readonly) NSRunLoopMode mode; -- (void)displayLinkFired; +- (void)displayLinkFiredWithDuration:(CFTimeInterval)duration; @end @@ -27,8 +27,9 @@ static CVReturn displayLinkFired (CVDisplayLinkRef displayLink, CVOptionFlags *flagsOut, void *displayLinkContext) { + CFTimeInterval duration = inOutputTime->videoRefreshPeriod / (inOutputTime->videoTimeScale * inOutputTime->rateScalar); PINDisplayLink *link = (__bridge PINDisplayLink *)displayLinkContext; - [link displayLinkFired]; + [link displayLinkFiredWithDuration:duration]; return kCVReturnSuccess; } @@ -38,6 +39,7 @@ @implementation PINDisplayLink BOOL _paused; NSInteger _frameInterval; + CFTimeInterval _duration; } + (PINDisplayLink *)displayLinkWithTarget:(id)target selector:(SEL)sel @@ -63,9 +65,10 @@ - (void)dealloc } } -- (void)displayLinkFired +- (void)displayLinkFiredWithDuration:(CFTimeInterval)duration { dispatch_async(dispatch_get_main_queue(), ^{ + self.duration = duration; [self.runloop performSelector:self.selector target:self.target argument:self order:NSUIntegerMax modes:@[self.mode]]; }); } @@ -111,6 +114,18 @@ - (void)setPaused:(BOOL)paused } } +- (CFTimeInterval)duration +{ + PINAssertMain(); + return _duration; +} + +- (void)setDuration:(CFTimeInterval)duration +{ + PINAssertMain(); + _duration = duration; +} + - (NSInteger)frameInterval { PINAssertMain(); From 1eb9e57e45b6f720982073ac67a9c0d370cca573 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sun, 24 May 2020 18:14:35 +0800 Subject: [PATCH 2/3] Remove null group --- PINRemoteImage.xcodeproj/project.pbxproj | 8 -------- 1 file changed, 8 deletions(-) diff --git a/PINRemoteImage.xcodeproj/project.pbxproj b/PINRemoteImage.xcodeproj/project.pbxproj index 6f680660..a17acc9f 100644 --- a/PINRemoteImage.xcodeproj/project.pbxproj +++ b/PINRemoteImage.xcodeproj/project.pbxproj @@ -1135,13 +1135,6 @@ path = AnimatedImages; sourceTree = ""; }; - 68C155D31F6EE3BE0075EE40 /* AnimatedImages */ = { - isa = PBXGroup; - children = ( - ); - path = AnimatedImages; - sourceTree = ""; - }; 68CA92801DB19C06008BECE2 /* PINCache */ = { isa = PBXGroup; children = ( @@ -1204,7 +1197,6 @@ F165DFD71BD021EE0008C6E8 /* Project */ = { isa = PBXGroup; children = ( - 68C155D31F6EE3BE0075EE40 /* AnimatedImages */, F1B918DD1BCF23C800710963 /* Categories */, 689613E2208FD90A00D2095C /* PINDisplayLink.h */, 689613E3208FD90B00D2095C /* PINDisplayLink.m */, From a2a3eda82eac5daadaf62ffbcf4936f0f58a37a2 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sun, 24 May 2020 18:19:27 +0800 Subject: [PATCH 3/3] Revert "Remove null group" This reverts commit 1eb9e57e45b6f720982073ac67a9c0d370cca573. --- PINRemoteImage.xcodeproj/project.pbxproj | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/PINRemoteImage.xcodeproj/project.pbxproj b/PINRemoteImage.xcodeproj/project.pbxproj index a17acc9f..6f680660 100644 --- a/PINRemoteImage.xcodeproj/project.pbxproj +++ b/PINRemoteImage.xcodeproj/project.pbxproj @@ -1135,6 +1135,13 @@ path = AnimatedImages; sourceTree = ""; }; + 68C155D31F6EE3BE0075EE40 /* AnimatedImages */ = { + isa = PBXGroup; + children = ( + ); + path = AnimatedImages; + sourceTree = ""; + }; 68CA92801DB19C06008BECE2 /* PINCache */ = { isa = PBXGroup; children = ( @@ -1197,6 +1204,7 @@ F165DFD71BD021EE0008C6E8 /* Project */ = { isa = PBXGroup; children = ( + 68C155D31F6EE3BE0075EE40 /* AnimatedImages */, F1B918DD1BCF23C800710963 /* Categories */, 689613E2208FD90A00D2095C /* PINDisplayLink.h */, 689613E3208FD90B00D2095C /* PINDisplayLink.m */,