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);
相关推荐
2501_915909062 小时前
不用越狱就看不到 iOS App 内部文件?使用 Keymob 查看和导出应用数据目录
android·ios·小程序·https·uni-app·iphone·webview
@大迁世界3 小时前
液态玻璃屏正在侵蚀你的电池
macos·ios·objective-c·cocoa
pop_xiaoli4 小时前
【iOS】类与对象底层
macos·ios·objective-c·cocoa·xcode
sp42a4 小时前
NativeScript iOS 平台开发技巧
ios·nativescript·app 开发
2501_915921434 小时前
常用iOS性能测试工具大全及使用指南
android·测试工具·ios·小程序·uni-app·cocoa·iphone
for_ever_love__5 小时前
Objecgtive-C学习实例对象,类对象, 元类对象与 isa指针
c语言·学习·ios
一招定胜负5 小时前
视频转写+LLM分析:课堂录音自动化处理实现
macos·ios·xcode
2501_915918416 小时前
有没有Xcode 替代方案?在快蝎 IDE 中完成 iOS 开发的过程
ide·vscode·ios·个人开发·xcode·swift·敏捷流程
blackorbird6 小时前
通过攻陷合法网站传播的新型iOS漏洞利用工具包DarkSword
macos·ios·objective-c·cocoa
for_ever_love__8 小时前
Objective-C学习 NSSet 和 NSMutableSet 功能详解
开发语言·学习·ios·objective-c