核心图形从后台线程绘制NSString

时间:2012-10-16 11:11:33

标签: nsstring core-graphics

我使用此代码在UILabel上绘制了一个NSString(我已经将其子类化)与队列中的Core Graphics一起绘制,但屏幕上没有显示任何内容。我没有错。

我从队列中调用这样的函数。

[label1 createtext];

-(void)createtext
{          
    UIFont *font=[UIFont fontWithName:@"Times New Roman" size:15.0];
    //Core Text (Create Attributed String)

    NSMutableArray *Text=[globaltextarray Text];
    CGSize size=[[Text objectAtIndex:0] sizeWithFont:font];

    if (UIGraphicsBeginImageContextWithOptions != NULL)
        UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
    else
        // iOS is < 4.0
        UIGraphicsBeginImageContext(size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    if (ctx != NULL)
        NSLog(@"not null context");
    UIColor *textColor = [UIColor blackColor];
    CGColorRef color = textColor.CGColor;

    CTFontRef font = CTFontCreateWithName((CFStringRef) @"TimesNewRomanPSMT", 15.0, NULL);

    CTTextAlignment theAlignment = kCTLeftTextAlignment;

    CFIndex theNumberOfSettings = 1;
    CTParagraphStyleSetting theSettings[1] = {
        { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),
            &theAlignment }
    };

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, theNumberOfSettings);

    NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:(id)font, (NSString *)kCTFontAttributeName,color, (NSString *)kCTForegroundColorAttributeName,paragraphStyle, (NSString *) kCTParagraphStyleAttributeName,nil];

    NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:[NSString stringWithCString:[[Text objectAtIndex:indexpath1.row] cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding] attributes:attributesDict];

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)stringToDraw);

    //Prepare our view for drawing

    CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
    CGContextTranslateCTM(ctx, 0, ([self bounds]).size.height );
    CGContextScaleCTM(ctx, 1.0, -1.0);

    //Create Frame
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect rect  = self.frame;
    CGPathAddRect(path, NULL, rect);

    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

    //Draw Frame
    CTFrameDraw(frame, ctx);

    UIGraphicsEndImageContext();
    //Release all retained objects
    CFRelease(framesetter);
    CFRelease(path);
    [stringToDraw release];
}

任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:1)

您正在创建一个临时图形上下文,绘制它,然后丢弃它。你永远不会告诉UIKit在屏幕上放任何东西。

您还需要

例如:

// In your class's declaration, define an image view to show your drawing.
// Make sure that this view is created (either as part of a storyboard/nib, or with explicit code)
@property (nonatomic) UIImageView *imageView;

- (void)createText
{
    CGSize size = /* you decide */;
    UIGraphicsBeginImageContext(size);

    // draw into the graphics context
    // after you're done:

    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    dispatch_async(dispatch_get_main_queue(), ^{
        self.imageView.image = image;
    });
}

答案 1 :(得分:-2)

您没有使用CoreGraphics框架绘图,您使用UIKit进行绘制,并且在后台线程中不允许这样做。您只能从主线程调用UIKit方法。例如,您正在调用UIGraphicsBeginImageContext(),但这是UIKit的一部分。无论如何,你不可能从后台线程中渲染窗口小部件代码中获得任何好处,除非你正在做一些真正断开连接的离线渲染(比如创建PDF或其他东西),那么你应该只在主线程中渲染你的小部件。