From 7ba84882641f50cdd724fa1a33d77a6655ddc877 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Mon, 8 Jul 2019 12:09:05 +0800 Subject: [PATCH 1/3] Fixes AnimatedImageView designated inilializer not work --- .../AnimatedImages/PINAnimatedImageView.m | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/Source/Classes/AnimatedImages/PINAnimatedImageView.m b/Source/Classes/AnimatedImages/PINAnimatedImageView.m index f0de9133..ab42cf20 100644 --- a/Source/Classes/AnimatedImages/PINAnimatedImageView.m +++ b/Source/Classes/AnimatedImages/PINAnimatedImageView.m @@ -61,6 +61,31 @@ - (void)commonInit:(PINCachedAnimatedImage *)animatedImage { _animatedImage = animatedImage; _animatedImageRunLoopMode = NSRunLoopCommonModes; + + [self initializeAnimatedImage:animatedImage]; +} + +- (void)initializeAnimatedImage:(PINCachedAnimatedImage *)animatedImage +{ + PINWeakify(self); + animatedImage.coverImageReadyCallback = ^(PINImage *coverImage) { + dispatch_async(dispatch_get_main_queue(), ^{ + PINStrongify(self); + // In this case the lock is already gone we have to call the unlocked version therefore + [self coverImageCompleted:coverImage]; + }); + }; + + animatedImage.playbackReadyCallback = ^{ + dispatch_async(dispatch_get_main_queue(), ^{ + // In this case the lock is already gone we have to call the unlocked version therefore + PINStrongify(self); + [self checkIfShouldAnimate]; + }); + }; + if (animatedImage.playbackReady) { + [self checkIfShouldAnimate]; + } } - (void)dealloc @@ -84,25 +109,7 @@ - (void)setAnimatedImage:(PINCachedAnimatedImage *)animatedImage _animatedImage = animatedImage; if (animatedImage != nil) { - PINWeakify(self); - animatedImage.coverImageReadyCallback = ^(PINImage *coverImage) { - dispatch_async(dispatch_get_main_queue(), ^{ - PINStrongify(self); - // In this case the lock is already gone we have to call the unlocked version therefore - [self coverImageCompleted:coverImage]; - }); - }; - - animatedImage.playbackReadyCallback = ^{ - dispatch_async(dispatch_get_main_queue(), ^{ - // In this case the lock is already gone we have to call the unlocked version therefore - PINStrongify(self); - [self checkIfShouldAnimate]; - }); - }; - if (animatedImage.playbackReady) { - [self checkIfShouldAnimate]; - } + [self initializeAnimatedImage:animatedImage]; } else { // Clean up after ourselves. self.layer.contents = nil; From 70dfa21ac24058bb82bdbd52218754744a81a50c Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Wed, 10 Jul 2019 22:30:28 +0800 Subject: [PATCH 2/3] Add test case --- .../AnimatedImages/PINAnimatedImageView.m | 6 +++-- Tests/PINAnimatedImageTests.swift | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Source/Classes/AnimatedImages/PINAnimatedImageView.m b/Source/Classes/AnimatedImages/PINAnimatedImageView.m index ab42cf20..ae5779f0 100644 --- a/Source/Classes/AnimatedImages/PINAnimatedImageView.m +++ b/Source/Classes/AnimatedImages/PINAnimatedImageView.m @@ -62,10 +62,12 @@ - (void)commonInit:(PINCachedAnimatedImage *)animatedImage _animatedImage = animatedImage; _animatedImageRunLoopMode = NSRunLoopCommonModes; - [self initializeAnimatedImage:animatedImage]; + if (animatedImage) { + [self initializeAnimatedImage:animatedImage]; + } } -- (void)initializeAnimatedImage:(PINCachedAnimatedImage *)animatedImage +- (void)initializeAnimatedImage:(nonnull PINCachedAnimatedImage *)animatedImage { PINWeakify(self); animatedImage.coverImageReadyCallback = ^(PINImage *coverImage) { diff --git a/Tests/PINAnimatedImageTests.swift b/Tests/PINAnimatedImageTests.swift index 7bc72458..7051cb5f 100644 --- a/Tests/PINAnimatedImageTests.swift +++ b/Tests/PINAnimatedImageTests.swift @@ -132,4 +132,29 @@ class PINAnimatedImageTests: XCTestCase, PINRemoteImageManagerAlternateRepresent } self.waitForExpectations(timeout: self.timeoutInterval(), handler: nil) } + + func testAnimatedImageViewInitializer() { + let animatedExpectation = self.expectation(description: "Animated image should be downloaded") + let imageManager = PINRemoteImageManager.init(sessionConfiguration: nil, alternativeRepresentationProvider: self) + imageManager.downloadImage(with: self.slowAnimatedGIFURL()!) { (result : PINRemoteImageManagerResult) in + XCTAssert(result.image == nil) + guard let animatedData = result.alternativeRepresentation as? NSData else { + XCTAssert(false, "alternativeRepresentation should be able to be coerced into data") + return + } + + XCTAssert(animatedData.pin_isGIF() && animatedData.pin_isAnimatedGIF()) + + DispatchQueue.main.async { + let pinCachedAnimatedImage = PINCachedAnimatedImage(animatedImageData: animatedData as Data) + + let gifImageView = PINAnimatedImageView(animatedImage: pinCachedAnimatedImage!) + XCTAssert(gifImageView.animatedImage?.coverImageReadyCallback != nil) + + animatedExpectation.fulfill() + } + } + + self.waitForExpectations(timeout: self.timeoutInterval(), handler: nil) + } } From efccfef9c9cd155ec93670d69f696e037a56d916 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 11 Jul 2019 15:11:49 +0800 Subject: [PATCH 3/3] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36c90fdf..a543c011 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - [new] Add PINRemoteImageManagerConfiguration configuration object. [#492](https://github.com/pinterest/PINRemoteImage/pull/492) [rqueue](https://github.com/rqueue) - [fixed] Fixes blending in animated WebP images. [#507](https://github.com/pinterest/PINRemoteImage/pull/507) [garrettmoon](https://github.com/garrettmoon) - [fixed] Fixes support in PINAnimatedImageView for WebP animated images. [#507](https://github.com/pinterest/PINRemoteImage/pull/507) [garrettmoon](https://github.com/garrettmoon) +- [fixed] Fixes AnimatedImageView designated initializer not work. [#512](https://github.com/pinterest/PINRemoteImage/pull/512) [zhongwuzw](https://github.com/zhongwuzw) ## 3.0.0 Beta 14 - [fixed] Re-enable warnings check [#506](https://github.com/pinterest/PINRemoteImage/pull/506) [garrettmoon](https://github.com/garrettmoon)