我在drawRect:
中绘制了一些文本。首先计算文本高度,然后使用drawInRect:
方法进行绘制。以下代码可行:
- (void)drawRect:(CGRect)rect
{
CGFloat titleHeight = [self heightForText:_entry.title
withFont:[UIFont systemFontOfSize:12.0f]];
CGRect r = CGRectMake(54, 6, kCellTextWidth, titleHeight);
[_entry.title drawInRect:r withFont:[UIFont titleFont]];
}
然后我使用dispatch_async
计算文本高度并在主队列中使用drawInRect绘制,但失败了:
- (void)drawRect:(CGRect)rect
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
CGFloat titleHeight = [self heightForText:_entry.title
withFont:[UIFont systemFontOfSize:12.0f]];
dispatch_async(dispatch_get_main_queue(), ^{
CGRect r = CGRectMake(54, 6, kCellTextWidth, titleHeight);
[_entry.title drawInRect:r withFont:[UIFont titleFont]];
});
});
}
错误:
<Error>: CGContextSetFont: invalid context 0x0
<Error>: CGContextSetTextMatrix: invalid context 0x0
<Error>: CGContextSetFontSize: invalid context 0x0
<Error>: CGContextSetTextPosition: invalid context 0x0
<Error>: CGContextShowGlyphsWithAdvances: invalid context 0x0
这个错误是什么意思?如何在另一个线程中计算文本高度以提高速度?
谢谢。