Merge pull request #2706 from dreampiggy/bugfix_mac_SDAnimatedImage_NSImage

Fix the bug of SDAnimatedImageView, which can not render the normal NSImage (including animated image) at all. Only `SDAnimatedImage` can be rendered
This commit is contained in:
DreamPiggy 2019-04-25 14:43:24 +08:00 committed by GitHub
commit 4dce559d44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 3 deletions

View File

@ -29,13 +29,16 @@
// For animated GIF rendering, set `animates` to YES or will only show the first frame
self.imageView2.animates = YES; // `SDAnimatedImageRep` can be used for built-in `NSImageView` to support better GIF & APNG rendering as well. No need `SDAnimatedImageView`
self.imageView3.animates = YES;
self.imageView4.animates = YES;
// NSImageView + Static Image
self.imageView1.sd_imageIndicator = SDWebImageProgressIndicator.defaultIndicator;
[self.imageView1 sd_setImageWithURL:[NSURL URLWithString:@"https://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Landscape_2.jpg"] placeholderImage:nil options:SDWebImageProgressiveLoad];
// NSImageView + Animated Image
[self.imageView2 sd_setImageWithURL:[NSURL URLWithString:@"https:raw.githubusercontent.com/onevcat/APNGKit/master/TestImages/APNG-cube.apng"]];
[self.imageView3 sd_setImageWithURL:[NSURL URLWithString:@"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif"]];
self.imageView4.wantsLayer = YES;
// SDAnimatedImageView + Static Image
[self.imageView3 sd_setImageWithURL:[NSURL URLWithString:@"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png"]];
// SDAnimatedImageView + Animated Image
self.imageView4.sd_imageTransition = SDWebImageTransition.fadeTransition;
[self.imageView4 sd_setImageWithURL:[NSURL URLWithString:@"http://littlesvr.ca/apng/images/SteamEngine.webp"] placeholderImage:nil options:SDWebImageForceTransition];

View File

@ -678,6 +678,17 @@ static NSUInteger SDDeviceFreeMemory() {
[super updateLayer];
}
}
- (BOOL)wantsUpdateLayer {
// AppKit is different from UIKit, it need extra check before the layer is updated
// When we use the custom animation, the layer.setNeedsDisplay is directly called from display link (See `displayDidRefresh:`). However, for normal image rendering, we must implements and return YES to mark it need display
if (_currentFrame) {
return NO;
} else {
return YES;
}
}
#endif

View File

@ -137,6 +137,18 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
expect(imageView.intrinsicContentSize).equal(image.size);
}
- (void)test12AnimatedImageViewLayerContents {
// Test that SDAnimatedImageView with built-in UIImage/NSImage will actually setup the layer for display
SDAnimatedImageView *imageView = [SDAnimatedImageView new];
UIImage *image = [[UIImage alloc] initWithData:[self testJPEGData]];
imageView.image = image;
#if SD_MAC
expect(imageView.wantsUpdateLayer).beTruthy();
#else
expect(imageView.layer.contents).notTo.beNil();
#endif
}
- (void)test20AnimatedImageViewRendering {
XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView rendering"];
SDAnimatedImageView *imageView = [[SDAnimatedImageView alloc] init];