异步绘制UILabel崩溃

时间:2017-03-29 03:25:44

标签: ios objective-c asynchronous uilabel

我尝试在异步线程中绘制UILabel内容,并遇到一些崩溃,Crashed: async_display.queue :: NSOperation 0x17444b5e0 (QOS: USER_INTERACTIVE) EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000058,下面是示例代码:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    NSLayoutManager *layoutManager = [NSLayoutManager new];
    layoutManager.usesFontLeading = NO;
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:text];
    [textStorage addLayoutManager:layoutManager];
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:contentSize];
    textContainer.lineFragmentPadding = 0;
    textContainer.maximumNumberOfLines = 0;
    [layoutManager addTextContainer:textContainer];
    [layoutManager ensureLayoutForTextContainer:textContainer];
    CGPoint glyphsPosition = CGPointZero;
    CGRect textBounds = [layoutManager usedRectForTextContainer:textContainer];
    UIGraphicsBeginImageContextWithOptions(contentSize, NO, 0.0);

    NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
    [layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:glyphsPosition];
    [layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:glyphsPosition];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
  });
}

[layoutManager ensureLayoutForTextContainer:textContainer];行崩溃,崩溃信息:

0  UIFoundation                   0x187486768 -[NSATSLineFragment getTypographicLineHeight:baselineOffset:leading:] + 116
1  UIFoundation                   0x18747d1e0 -[NSATSTypesetter _layoutLineFragmentStartingWithGlyphAtIndex:characterIndex:atPoint:renderingContext:] + 2392
2  UIFoundation                   0x18747e758 -[NSATSTypesetter layoutParagraphAtPoint:] + 160
3  UIFoundation                   0x1874d4b34 -[NSTypesetter _layoutGlyphsInLayoutManager:startingAtGlyphIndex:maxNumberOfLineFragments:maxCharacterIndex:nextGlyphIndex:nextCharacterIndex:] + 2948
4  UIFoundation                   0x1874d5cd8 -[NSTypesetter layoutCharactersInRange:forLayoutManager:maximumNumberOfLineFragments:] + 244
5  UIFoundation                   0x18747f1b8 -[NSATSTypesetter layoutCharactersInRange:forLayoutManager:maximumNumberOfLineFragments:] + 448
6  UIFoundation                   0x18746b2ec -[NSLayoutManager(NSPrivate) _fillLayoutHoleForCharacterRange:desiredNumberOfLines:isSoft:] + 1116
7  UIFoundation                   0x18746cde0 -[NSLayoutManager(NSPrivate) _fillLayoutHoleAtIndex:desiredNumberOfLines:] + 196
8  UIFoundation                   0x187479a94 _NSFastFillAllLayoutHolesUpToEndOfContainerForGlyphIndex + 1252
9  UIFoundation                   0x1874a2100 -[NSLayoutManager ensureLayoutForTextContainer:] + 380
10 project                          0x10056d384 __36-[AsyncDisplayLabel displayBlock]_block_invoke (ESPAsyncDisplayLabel.m:112)
11 project                          0x10056db4c __29-[AsyncLayer asyncDisplay]_block_invoke.73 (ESPAsyncLayer.m:93)

我尝试使用pthread_mutex_t锁定NSTextKit init,但总是遇到崩溃......

0 个答案:

没有答案