基准UIView drawRect:方法

Kyl*_*yle 6 iphone benchmarking cocoa-touch objective-c quartz-graphics

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

有什么建议?

谢谢,

凯尔

rpe*_*ich 5

您可以通过执行以下操作来为视图建立基准:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

(没试过,但它应该工作)