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);
相关推荐
咕噜企业签名分发-淼淼3 小时前
开发源码搭建一码双端应用分发平台教程:逐步分析注意事项
android·ios
键盘敲没电14 小时前
【IOS】GCD学习
学习·ios·objective-c·xcode
SY.ZHOU14 小时前
Significant Location Change
macos·ios·cocoa
吴Wu涛涛涛涛涛Tao21 小时前
深入理解 Swift Codable:从基础到进阶
ios
Jouzzy1 天前
【iOS安全】iPhone X iOS 16.7.11 (20H360) WinRa1n 越狱教程
安全·ios·iphone
二流小码农2 天前
鸿蒙开发:实现一个标题栏吸顶
android·ios·harmonyos
season_zhu2 天前
iOS开发:关于日志框架
ios·架构·swift
Digitally2 天前
如何在电脑上轻松访问 iPhone 文件
ios·电脑·iphone
安和昂2 天前
【iOS】YYModel源码解析
ios
pop_xiaoli2 天前
UI学习—cell的复用和自定义cell
学习·ui·ios