diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fdf74f2..5490ed61 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) - [fixed] Set bpp(bits per pixel) to 32 bit for GIF. [#511](https://github.com/pinterest/PINRemoteImage/pull/511) [zhongwuzw](https://github.com/zhongwuzw) - [new] Add cancel method for PINRemoteImageManager. [#509](https://github.com/pinterest/PINRemoteImage/pull/509) [zhongwuzw](https://github.com/zhongwuzw) - [fixed] Fixes build error when using Xcode 10.2.1. [#524](https://github.com/pinterest/PINRemoteImage/pull/524) [ANNotunzdY](https://github.com/ANNotunzdY) diff --git a/Source/Classes/AnimatedImages/PINAnimatedImageView.m b/Source/Classes/AnimatedImages/PINAnimatedImageView.m index 53823bdf..8ef426fc 100644 --- a/Source/Classes/AnimatedImages/PINAnimatedImageView.m +++ b/Source/Classes/AnimatedImages/PINAnimatedImageView.m @@ -61,6 +61,33 @@ - (void)commonInit:(PINCachedAnimatedImage *)animatedImage { _animatedImage = animatedImage; _animatedImageRunLoopMode = NSRunLoopCommonModes; + + if (animatedImage) { + [self initializeAnimatedImage:animatedImage]; + } +} + +- (void)initializeAnimatedImage:(nonnull 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 +111,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; diff --git a/Tests/PINAnimatedImageTests.swift b/Tests/PINAnimatedImageTests.swift index 66d91f7d..5c31b6d2 100644 --- a/Tests/PINAnimatedImageTests.swift +++ b/Tests/PINAnimatedImageTests.swift @@ -132,8 +132,33 @@ 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) + } - func testGIFBytes() { + func testGIFBytes() { 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