iOS 14下面图片无法加载,包括weex、YYAnimateView、SDAnimatedImageView

升级xcode12后,编译运行App发现大片大片的图片加载不出来,包括weex的图片和YYAnimateView的图片都有问题。

经过一番研究之后,发现是iOS 14下UIKit对 displayLayer:的处理机制有所变化。
displayLayer:CALayerDelegate的代理方法。在iOS 14之前,UIKit在调用这个方法之前就会去渲染UIImageView.image
而在iOS 14,UIKit则是先去调用代理方法,如果你实现了displayLayer:这个方法,那么UIKit就不会再去渲染了。

如果改成下面这样就可以正常加载了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (void)displayLayer:(CALayer *)layer {
UIImage *currentFrame = _curFrame;
if (currentFrame) {
layer.contentsScale = currentFrame.scale;
layer.contents = (__bridge id)currentFrame.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];
}
}
// if (_curFrame) {
// layer.contents = (__bridge id)_curFrame.CGImage;
// }
}

参考:

  1. https://github.com/apache/incubator-weex/issues/3265
  2. https://github.com/ibireme/YYWebImage/issues/242
  3. https://github.com/SDWebImage/SDWebImage/issues/3040
Author: y500
Link: https://www.y500.me/2020/09/29/image-not-render-on-ios14/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.