当前位置: 代码迷 >> 综合 >> iOS14 YYAnimatedImageView图片不显示,多数用在YYLabel的富文本中添加的图片
  详细解决方案

iOS14 YYAnimatedImageView图片不显示,多数用在YYLabel的富文本中添加的图片

热度:11   发布时间:2023-12-22 17:36:33.0

方案一 简单粗暴

// 直接在YYAnimatedImageView上添加一个UIImageView
/// @param yyimage YYAnimatedImageView
/// @param size UIImageView的大小
+(UIImageView *)addYYimageSubImageView:(YYAnimatedImageView *)yyimage size:(CGSize)size
{UIImageView *imView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];imView.image = yyimage.image;[yyimage addSubview:imView];return imView;
}

方案二  创建YYAnimatedImageView的category在load实现交换方法(从根本上解决这个bug)

.h

#import "YYAnimatedImageView.h"NS_ASSUME_NONNULL_BEGIN@interface YYAnimatedImageView (ImageShow)@endNS_ASSUME_NONNULL_END

.m

+ (void)load
{// 获取系统的方法Method displayLayerMethod = class_getInstanceMethod(self, @selector(displayLayer:));// 获取更新的方法Method displayLayerNewMethod = class_getInstanceMethod(self, @selector(displayLayerNew:));// 方法交换method_exchangeImplementations(displayLayerMethod, displayLayerNewMethod);
}- (void)displayLayerNew:(CALayer *)layer
{Ivar imageIvar = class_getInstanceVariable([self class], "_curFrame");UIImage *image = object_getIvar(self, imageIvar);if (image){layer.contents = (__bridge id)image.CGImage;}else{if (@available(iOS 14.0, *)){[super displayLayer:layer];}}
}