diff --git a/SDWebImage/Core/SDAnimatedImagePlayer.m b/SDWebImage/Core/SDAnimatedImagePlayer.m index c15a99db..2efd8805 100644 --- a/SDWebImage/Core/SDAnimatedImagePlayer.m +++ b/SDWebImage/Core/SDAnimatedImagePlayer.m @@ -108,6 +108,7 @@ if (!_displayLink) { _displayLink = [SDDisplayLink displayLinkWithTarget:self selector:@selector(displayDidRefresh:)]; [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:self.runLoopMode]; + [_displayLink stop]; } return _displayLink; } @@ -197,7 +198,7 @@ } - (BOOL)isPlaying { - return self.displayLink.isRunning; + return _displayLink.isRunning; } - (void)seekToFrameAtIndex:(NSUInteger)index loopCount:(NSUInteger)loopCount { diff --git a/SDWebImage/Core/SDAnimatedImageView.m b/SDWebImage/Core/SDAnimatedImageView.m index c77fe2e8..181f2db9 100644 --- a/SDWebImage/Core/SDAnimatedImageView.m +++ b/SDWebImage/Core/SDAnimatedImageView.m @@ -178,11 +178,8 @@ // Ensure disabled highlighting; it's not supported (see `-setHighlighted:`). super.highlighted = NO; - // Update should animate - [self updateShouldAnimate]; - if (self.shouldAnimate) { - [self startAnimating]; - } + // Start animating + [self startAnimating]; [self.imageViewLayer setNeedsDisplay]; } @@ -312,7 +309,10 @@ - (void)startAnimating { if (self.player) { - [self.player startPlaying]; + [self updateShouldAnimate]; + if (self.shouldAnimate) { + [self.player startPlaying]; + } } else { #if SD_UIKIT [super startAnimating]; diff --git a/Tests/Tests/SDAnimatedImageTest.m b/Tests/Tests/SDAnimatedImageTest.m index 1c7b8bcf..b3fb53f2 100644 --- a/Tests/Tests/SDAnimatedImageTest.m +++ b/Tests/Tests/SDAnimatedImageTest.m @@ -108,12 +108,7 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun SDAnimatedImage *image = [SDAnimatedImage imageWithData:[self testGIFData]]; imageView.image = image; expect(imageView.image).notTo.beNil(); -#if SD_MAC - imageView.animates = YES; -#else - [imageView startAnimating]; -#endif - expect(imageView.currentFrame).notTo.beNil(); // current frame + expect(imageView.player).notTo.beNil(); } - (void)test09AnimatedImageViewSetAnimatedImageAPNG { @@ -121,12 +116,7 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun SDAnimatedImage *image = [SDAnimatedImage imageWithData:[self testAPNGPData]]; imageView.image = image; expect(imageView.image).notTo.beNil(); - #if SD_MAC - imageView.animates = YES; - #else - [imageView startAnimating]; - #endif - expect(imageView.currentFrame).notTo.beNil(); // current frame + expect(imageView.player).notTo.beNil(); } - (void)test10AnimatedImageInitWithCoder { @@ -187,6 +177,27 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun expect(imageView.image).equal(image); } +- (void)test14AnimatedImageViewStopPlayingWhenHidden { + SDAnimatedImageView *imageView = [SDAnimatedImageView new]; +#if SD_UIKIT + [self.window addSubview:imageView]; +#else + [self.window.contentView addSubview:imageView]; +#endif + SDAnimatedImage *image = [SDAnimatedImage imageWithData:[self testGIFData]]; + imageView.image = image; +#if SD_UIKIT + [imageView startAnimating]; +#else + imageView.animates = YES; +#endif + SDAnimatedImagePlayer *player = imageView.player; + expect(player).notTo.beNil(); + expect(player.isPlaying).beTruthy(); + imageView.hidden = YES; + expect(player.isPlaying).beFalsy(); +} + - (void)test20AnimatedImageViewRendering { XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView rendering"]; SDAnimatedImageView *imageView = [[SDAnimatedImageView alloc] init]; @@ -348,7 +359,7 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun [self waitForExpectationsWithCommonTimeout]; } -- (void)test25AnimatedImageStopAnimatingClearBuffer { +- (void)test26AnimatedImageStopAnimatingClearBuffer { XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView stopAnimating clear buffer when stopped"]; SDAnimatedImageView *imageView = [SDAnimatedImageView new];