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 方法中。
原代码:

	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);

修复后的代码:

 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);
在Bison中,yytext是一个全局变量,用于存放当前解析的token的文本内容。通常情况下,我们可以在Bison的语法规则中通过使用$1、$2等符号来引用yytext,这些符号代表了当前解析的token的文本内容。 例如,假设我们需要解析一个简单的表达式,表达式中只包含加、减、乘、除四种运算符和数字。那么我们可以定义如下的Bison语法规则: ``` %{ #include <stdio.h> %} %token NUMBER %token PLUS %token MINUS %token TIMES %token DIVIDE %% expr: NUMBER | expr PLUS expr | expr MINUS expr | expr TIMES expr | expr DIVIDE expr ; %% int main() { yyparse(); return 0; } int yyerror(const char *msg) { fprintf(stderr, "Error: %s\n", msg); return 0; } int yylex() { int c = getchar(); if (isdigit(c)) { ungetc(c, stdin); scanf("%d", &yylval); return NUMBER; } else if (c == '+') { return PLUS; } else if (c == '-') { return MINUS; } else if (c == '*') { return TIMES; } else if (c == '/') { return DIVIDE; } else if (c == '\n' || c == EOF) { return 0; } else { return -1; } } ``` 在上述代码中,我们定义了一个简单的表达式语法规则,其中每个token都有一个相应的词法分析器函数yylex()来生成。同时,我们还定义了一个yyerror()函数,用于处理语法错误。当Bison解析表达式时,它会自动将当前解析的token的文本内容存放到yytext中,并将其传递到语法规则中。 例如,当我们输入一个简单的表达式"1+2"时,Bison会先调用yylex()函数来获取第一个token,也就是数字"1",然后将其存放到yylval中,并返回NUMBER这个token类型。接下来,Bison会继续调用yylex()函数来获取下一个token,也就是加号"+",然后将其存放到yytext中,并返回PLUS这个token类型。最后,Bison会根据语法规则来解析表达式,并输出结果"3"。 可以看到,在Bison中使用yytext非常简单,我们只需要在语法规则中使用相应的符号$1、$2等来引用yytext即可。如果需要在Bison的代码中直接访问yytext,则可以直接使用全局变量yytext来获取其值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值