iOS 14 YYAnimatedImageView加载图片失败处理
阅读原文时间:2023年07月09日阅读:2

1.问题出在YYAnimatedImageView源码一个方法中

- (void)displayLayer:(CALayer *)layer {

if (_curFrame) {

layer.contents = (__bridge id)_curFrame.CGImage;

}

}

2.解决办法

第一种方法:修改源码方法

- (void)displayLayer:(CALayer *)layer {

if (_curFrame) {

layer.contents = (__bridge id)_curFrame.CGImage;

}else{

if (@available(iOS 14.0, *)) {

[super displayLayer:layer];

}

}

}

第二种方法:运行时

#import "YYAnimatedImageView+iOS14.h"

#import

@implementation YYAnimatedImageView (iOS14)

+ (void)load {

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

Method method1;

Method method2;

method1 = class_getInstanceMethod([self class], @selector(lz_displayLayer:));

method2 = class_getInstanceMethod([self class], @selector(displayLayer:));

method_exchangeImplementations(method1, method2);

});

}

- (void)lz_displayLayer:(CALayer *)layer {

Ivar ivar = class_getInstanceVariable(self.class, "_curFrame");

UIImage *_curFrame = object_getIvar(self, ivar);

if (_curFrame) {

layer.contents = (__bridge id)_curFrame.CGImage;

}else{

if (@available(iOS 14.0, *)) {

[super displayLayer:layer];

}

}

}

@end