问题
更新iOS14
后发现原来使用的YBImageBrowser
浏览图片出现黑屏,使用YYImage
显示图片也出现了相同的黑屏问题。
原因⚠️:iOS 14
系统调用方法- (void)displayLayer:(CALayer *)layer
,YYAnimatedImageView
没有正确处理。
解决
方式一
-(void)displayLayer:(CALayer *)layer {
UIImage *currentFrame = _curFrame;
if (!currentFrame) {
currentFrame = self.image;
}
if (currentFrame) {
layer.contentsScale = currentFrame.scale;
layer.contents = (__bridge id)currentFrame.CGImage;
}
}
方式二
- (void)displayLayer:(CALayer *)layer {
if (_curFrame) {
layer.contents = (__bridge id)_curFrame.CGImage;
} else {
// If we have no animation frames, call super implementation. iOS 14+ UIImageView use this delegate method for rendering.
if ([UIImageView instancesRespondToSelector:@selector(displayLayer:)]) {
[super displayLayer:layer];
}
}
}
方式三
pod 'YYImage', :git => 'https://github.com/QiuYeHong90/YYImage.git'
方式四
#import "YYAnimatedImageView+Extension.h"
@implementation YYAnimatedImageView (Extension)
+(void)load
{
// hook:钩子函数
Method method1 = class_getInstanceMethod(self, @selector(displayLayer:));
Method method2 = class_getInstanceMethod(self, @selector(dx_displayLayer:));
method_exchangeImplementations(method1, method2);
}
-(void)dx_displayLayer:(CALayer *)layer {
if ([UIImageView instancesRespondToSelector:@selector(displayLayer:)]) {
[super displayLayer:layer];
}
}