【问题标题】:Draw NSString without using drawAtPoint or drawRectIn在不使用 drawAtPoint 或 drawRectIn 的情况下绘制 NSString
【发布时间】:2012-10-15 01:37:30
【问题描述】:

我对 UILabel 进行了子类化,并使用 drawrect 方法在屏幕上绘制字符串。这是我的代码:

- (void)drawRect:(CGRect)rect
{
    CGRect   aRect  = self.frame;
    NSMutableArray *Text=[globaltextarray Text];

   [[NSString stringWithCString:[[Text objectAtIndex:labelindexpath.row] cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding]  drawInRect:aRect
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        withFont:[UIFont fontWithName:@"Times New Roman" size:15.0] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
   [super drawRect:rect];     
}

它工作正常,但对于我的目的来说它很慢。我需要在不使用[Label setNeedDisplay]; 的情况下绘制字符串,因为需要从主线程调用 setNeedsDisplay。渲染工作需要在后台队列中完成。如果您能给我提供一个小例子,我将不胜感激。任何帮助表示赞赏。

谢谢。

【问题讨论】:

    标签: objective-c nsstring subclass drawrect


    【解决方案1】:

    您可以创建一个新的 CGContext 以在单独的线程上使用 CGBitmapContextCreateImage 将字符串绘制到 CGImage/UIImage 中,然后将位图(生成的图像)​​绘制到主视图中线程。

    编辑:post 为例。

    【讨论】:

    • 我尝试了这个例子,但是当我将生成的图像绘制到主线程时,使用 setNeedsDisplay 比以前太滞后了!我错过了什么吗?
    • 我认为有一个错误的答案,这样不会缩短出场所需的时间。首先应该使用 Instruments Time Profiler 来查找程序缓慢的原因。
    • 我已经这样做了,是我的子类 UILabel 中的 drawrect 导致了延迟滚动!
    • 在 UITableView 中?为什么不直接继承 UITableViewCell 和绘制字符串?
    • 在 UITableView 中是。好吧,我已经想到了,但是我必须更改很多代码...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 2018-07-15
    • 2021-04-03
    • 2018-06-27
    • 2013-07-20
    • 2021-01-03
    • 2018-12-28
    相关资源
    最近更新 更多