I have recently added some Core Graphics elements to my app and it has slowed dramatically, rendering it just about useless. I am not sure if I am just not using Core Graphics efficiently (how do I make it faster?) or if this problem is inherent in the use of Core Graphics. My code is below, after this description:
There are speech bubbles that move around the screen (in x & y). Each bubble has a corresponding image at the bottom of the screen that also moves in x & y. I am using Core Graphics to draw a triangle from the speech bubbles, terminating at the bottom images. There can be more than 10 of these triangles on the screen at once, all moving. Every 1/10th of a second a timer fires, calculates the positions of everything, and calls setNeedsDisplay
on the triangle object I have built.
Code for the triangle object:
- (void)drawRect:(CGRect)rect {
// Drawing code
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext(); //get the graphics context
CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1); //there are two relevant color states, "Stroke" -- used in Stroke drawing functions and "Fill" - used in fill drawing functions
//Create an array of points for triangle
CGPoint leftTrianglePoint;
CGPoint rightTrianglePoint;
rightTrianglePoint = CGPointMake(pointBlob.x, (pointBlob.y + 10.0));
leftTrianglePoint = CGPointMake(pointBlob.x, (pointBlob.y - 10.0));
CGPoint triangleArray [] =
{
pointGrid,
rightTrianglePoint,
leftTrianglePoint,
pointGrid,};
//Add lines to the context
CGContextAddLines( ctx, triangleArray, 4);
//draw the path
//color the context - using a function for gray, to matche the saved blobs
CGContextSetGrayFillColor(ctx, 0.0, .7);
//Now fill the path
CGContextFillPath(ctx);
}
How can I speed this up / build efficiency so my graphics move smoothly again?
Best Answer
Hate to say it, but drop Core Graphics and do it with OpenGL. OpenGL loves triangles. It will blit thousands per second for you at high speed. Many people think OpenGL is just for 3D stuff but what you are doing in 2D also works extremely well with it.