Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 为什么drawRect中的CGContextDrawImage最初速度较慢?_Iphone_Objective C_Ios_Core Graphics_Drawrect - Fatal编程技术网

Iphone 为什么drawRect中的CGContextDrawImage最初速度较慢?

Iphone 为什么drawRect中的CGContextDrawImage最初速度较慢?,iphone,objective-c,ios,core-graphics,drawrect,Iphone,Objective C,Ios,Core Graphics,Drawrect,在一个支持ARC的iOS应用程序中,考虑这个简单的UIView子类,该应用程序在触摸屏幕时绘制视图: - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint location = [[touches anyObject] locationInView:self]; int padding = brushSize / 2; CGRect brushRect = CGRectM

在一个支持ARC的iOS应用程序中,考虑这个简单的UIView子类,该应用程序在触摸屏幕时绘制视图:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    CGPoint location = [[touches anyObject] locationInView:self];

    int padding = brushSize / 2;

    CGRect brushRect = CGRectMake(
        location.x - padding, location.y - padding,
        brushSize, brushSize
    );

    [self setNeedsDisplayInRect:brushRect];

}

- (void)drawRect:(CGRect)rect {

    NSDate *start = [NSDate date];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, rect, brushImage);

    NSLog(@"%f", [start timeIntervalSinceNow]);

}
在本例中,
brushImage
是一个正方形位图,
brushSize
是一个描述位图宽度和高度的整数

我总是得到这样的日志输出:

-0.131658
-0.133998
-0.143314
-0.007132
-0.006968
-0.007444
-0.006733
-0.008574
-0.007163
-0.006560
-0.006958
对drawRect的第一次调用要比后续调用花费更长的时间才能完成,此后drawRect的速度一直很快。只有在视图初始化后的第一次调用中,drawRect才会变慢

我在模拟器和我尝试过的所有物理设备中都看到了类似的结果。初始调用通常需要更长的时间才能完成


为什么drawRect/CGContextDrawImage在初始调用时速度如此之慢?更重要的是,如何修复此问题?

检查rect。我打赌前三个是全屏更新。我以前遇到过这个问题。无论您做什么,IOS都会强制更新前几次全屏显示(可能是针对其GL缓冲系统)。如果你让它单独呆5秒钟,然后再画一些东西,你会得到同样的行为。我从来没有弄清楚真正的原因是什么,我怀疑我是否会:(


看看我最初问的问题:

谢谢!的确,iOS在最初几次通话中重新绘制了整个视图。感谢您的回答。干杯!