iOS 17.0 YYText UIGraphicsBeginImageContextWithOptions 崩溃处理

在iOS17上,YYText会报以下错误:

UIGraphicsBeginImageContext() failed to allocate CGBitampContext: size={0, 0}, scale=3.000000, bitmapInfo=0x2002. Use UIGraphicsImageRenderer to avoid this assert.

解决方法如下:

在YYTextAsyncLayer.m文件中,_displayAsync:(BOOL)async 方法中。

原代码:

objectivec 复制代码
	UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, self.contentsScale);
	CGContextRef context = UIGraphicsGetCurrentContext();
	if (self.opaque) {
		CGSize size = self.bounds.size;
		size.width *= self.contentsScale;
		size.height *= self.contentsScale;
		CGContextSaveGState(context); {
			if (!self.backgroundColor || CGColorGetAlpha(self.backgroundColor) < 1) {
				CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
				CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height));
				CGContextFillPath(context);
			}
			if (self.backgroundColor) {
				CGContextSetFillColorWithColor(context, self.backgroundColor);
				CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height));
				CGContextFillPath(context);
			}
		} CGContextRestoreGState(context);
	}
	task.display(context, self.bounds.size, ^{return NO;});
	UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	self.contents = (__bridge id)(image.CGImage);

修复后的代码:

objectivec 复制代码
 UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
        format.opaque = self.opaque;
        format.scale = self.contentsScale;

        UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:self.bounds.size format:format];
        UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
            CGContextRef context = rendererContext.CGContext;
            if (self.opaque) {
                CGSize size = self.bounds.size;
                size.width *= self.contentsScale;
                size.height *= self.contentsScale;
                CGContextSaveGState(context); {
                    if (!self.backgroundColor || CGColorGetAlpha(self.backgroundColor) < 1) {
                        CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
                        CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height));
                        CGContextFillPath(context);
                    }
                    if (self.backgroundColor) {
                        CGContextSetFillColorWithColor(context, self.backgroundColor);
                        CGContextAddRect(context, CGRectMake(0, 0, size.width, size.height));
                        CGContextFillPath(context);
                    }
                } CGContextRestoreGState(context);
            }
            task.display(context, self.bounds.size, ^{return NO;});
        }];

        self.contents = (__bridge id)(image.CGImage);
相关推荐
貂蝉空大1 小时前
uni-app开发安卓和ios app 真机调试
android·ios·uni-app
胖虎12 小时前
iOS 中的圆角与平滑圆角:从新特性到老项目适配
ios·圆角·平滑圆角·cornercurve
志飞2 小时前
ios UICollectionView使用自定义UICollectionViewCell
ios·collectionview·自定义cell
Neo Evolution8 小时前
Flutter与移动开发的未来:谷歌的技术愿景与实现路径
android·人工智能·学习·ios·前端框架·webview·着色器
没头脑的ht1 天前
ios App的启动过程和启动优化
ios
敲代码的鱼哇1 天前
设备唯一ID获取,支持安卓/iOS/鸿蒙Next(uni-device-id)UTS插件
android·ios·uniapp·harmonyos
江上清风山间明月1 天前
Flutter最简单的路由管理方式Navigator
android·flutter·ios·路由·页面管理·navigator
fangcaojushi1 天前
解决videojs在ios端视频无法播放的问题
ios·音视频
调皮的芋头2 天前
iOS各个证书生成细节
人工智能·ios·app·aigc
coooliang2 天前
【iOS】SwiftUI状态管理
ios·swiftui·swift