问题
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