Coregraphics causing big lag on iPad?

旧巷老猫 提交于 2019-12-12 01:48:49

问题


 UIGraphicsBeginImageContext(self.view.bounds.size);
        [currentStrokeImageView.image drawInRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), dWidth);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, 1.0f);
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), pointA.x, pointA.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), pointB.x, pointB.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        currentStrokeImageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

For some reason this runs with absolutely no lag on the iphone/ipod but on the iPad their is a significant lag while drawing. The code I use is above, any suggestions to fix this?


回答1:


The reason why this is so laggy is because you are doing it in the touchesMoved:withEvent:. This method can be called many, many times (obviously) while touch events are received. Because drawing to a graphics context can be a resource intensive operation, I would recommend not doing all the things you are doing there. I would, as much as possible, defer the rendering you are doing to the touchesBegin and touchesEnd methods. If that is not possible, perhaps you could only preform these operations once a certain delta has been reached in the movements, for example, every 2.0f points.



来源:https://stackoverflow.com/questions/7562413/coregraphics-causing-big-lag-on-ipad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!