Fix the macOS animated image rendering issue by using the correct layer

This commit is contained in:
DreamPiggy 2018-03-29 02:24:48 +08:00
parent a6d61c409d
commit 82e3779426
1 changed files with 15 additions and 9 deletions

View File

@ -13,6 +13,7 @@
#import "UIImage+WebCache.h"
#import "NSImage+Additions.h"
#import <mach/mach.h>
#import <objc/runtime.h>
#if SD_MAC
#import <CoreVideo/CoreVideo.h>
@ -142,12 +143,18 @@ dispatch_semaphore_signal(self->_lock);
#else
@property (nonatomic, strong) CADisplayLink *displayLink;
#endif
// Layer-backed NSImageView use a subview of `NSImageViewContainerView` to do actual layer rendering. We use this layer instead of `self.layer` during animated image rendering.
#if SD_MAC
@property (nonatomic, strong, readonly) CALayer *imageViewLayer;
#endif
@end
@implementation SDAnimatedImageView
#if SD_UIKIT
@dynamic animationRepeatCount;
#else
@dynamic imageViewLayer;
#endif
#pragma mark - Initializers
@ -396,6 +403,13 @@ dispatch_semaphore_signal(self->_lock);
}
#endif
#if SD_MAC
- (CALayer *)imageViewLayer {
NSView *imageView = objc_getAssociatedObject(self, NSSelectorFromString(@"_imageView"));
return imageView.layer;
}
#endif
#pragma mark - Life Cycle
- (void)dealloc
@ -776,7 +790,6 @@ dispatch_semaphore_signal(self->_lock);
#pragma mark - CALayerDelegate (Informal)
#pragma mark Providing the Layer's Content
#if SD_UIKIT
- (void)displayLayer:(CALayer *)layer
{
if (_currentFrame) {
@ -784,19 +797,12 @@ dispatch_semaphore_signal(self->_lock);
layer.contents = (__bridge id)_currentFrame.CGImage;
}
}
#endif
#if SD_MAC
- (BOOL)wantsUpdateLayer
{
return YES;
}
- (void)updateLayer
{
if (_currentFrame) {
self.layer.contentsScale = self.animatedImageScale;
self.layer.contents = (__bridge id)_currentFrame.CGImage;
[self displayLayer:self.imageViewLayer];
} else {
[super updateLayer];
}