首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Benchmark UIView drawRect:方法

Benchmark UIView drawRect:方法
EN

Stack Overflow用户
提问于 2009-05-12 00:42:57
回答 4查看 3K关注 0票数 6

我正在用Objective-C编写一个iPhone应用程序,它在视图中使用了一些自定义的绘图,我想对我的代码的各种修订进行基准测试,看看哪些是真正有帮助的。我打算这样做:设置一个新的应用程序,将我的自定义绘图代码添加到视图的drawRect:方法中,然后,在视图控制器的for循环中,多次发送[UIView setNeedsDisplay]并计算完成所需的时间。但是,setNeedsDisplay调用似乎被缓存了,所以即使我在for循环中调用了1000次,drawRect:方法也只被调用了一次。另外,我试着直接调用drawRect:,但是我需要一个图形上下文来做一些绘图,当我不使用setNeedsDisplay:时,UIGraphicsGetCurrentContext()不会给我一个上下文。

有什么建议吗?

谢谢,

凯尔

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-05-12 02:23:15

您可以通过执行以下操作对视图进行基准测试:

代码语言:javascript
复制
- (NSTimeInterval)timeTakenToDrawView:(UIView *)view count:(NSInteger)count
{
    CGRect bounds = [view bounds];
    NSDate *startDate = [NSDate date];
    UIGraphicsBeginImageContext(bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    NSInteger i = 0;
    for (i = 0; i < count; i++) {
        CGContextSaveGState(context);
        [view drawRect:bounds];
        CGContextRestoreGState(context);
    }
    UIGraphicsEndImageContext();
    return [[NSDate date] timeIntervalSinceDate:startDate];
}

(我还没试过,但应该可以用)

票数 5
EN

Stack Overflow用户

发布于 2009-05-12 01:07:48

在你的NSView的.m里面。显然,您可能希望将其连接到UI或其他什么地方。

代码语言:javascript
复制
- (void)awakeFromNib {
    // Have to wait for the window to be onscreen, graphics context ready, etc
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:NULL repeats:NO];
}


- (void)timerFired:(NSTimer *)aTimer {
    [self lockFocus];

    NSDate* then = [NSDate date];

    int callIdx = 0;
    for( callIdx = 0; callIdx < 1000; callIdx++ ) {
        [self drawRect:[self bounds]];
    }
    NSDate* now = [NSDate date];

    [self unlockFocus];

    NSLog(@"Took %f seconds", [now timeIntervalSinceDate:then]);
}

- (void)drawRect:(NSRect)rect {
    [[NSColor redColor] set];
    NSRectFill(rect);
}
票数 0
EN

Stack Overflow用户

发布于 2009-05-12 03:20:21

不要忘记,如果您的drawRect经常使用,您还可以使用CoreImage性能工具并获得帧率。

您还可以使用一些性能工具来查看在该方法中花费了多长时间。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/850677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档