Matthew
Matthew

Reputation: 31

Objective C displaying an array of images

So I'm creating a game that is supposed to have a dynamically generated coral reef. The reef has several objects that contain images (100x100) that I am trying to display. I can display the images but with severe CPU usage. The problem lies in my drawing code I believe:

-(void)drawCoralWithContext:(CGContextRef)ctx{
    for(Coral *c in coral){
        CGRect coralRect = CGRectMake (0, 0, [c size], [c size]);

        CGLayerRef layerRef = CGLayerCreateWithContext(ctx, CGSizeMake([c size], [c size]), NULL);
        CGContextRef layerCtx = CGLayerGetContext(layerRef);
        coralRect = CGRectMake (0, 0, [c size], [c size]);
        layerRef = CGLayerCreateWithContext(ctx, CGSizeMake([c size], [c size]), NULL);
        layerCtx = CGLayerGetContext(layerRef);
        CGContextDrawImage(layerCtx, coralRect, [c image]);
        CGContextSaveGState(ctx);
        CGContextTranslateCTM(ctx, c.position.x, c.position.y);
        CGContextDrawLayerAtPoint(ctx, [c position], layerRef);
        CGContextRestoreGState(ctx);
        CFRelease(layerRef);
    }
}

The context being passed in: CGContextRef ctx = UIGraphicsGetCurrentContext();

I need to reduce the amount of processing. Any help is greatly appreciated, thanks.

Upvotes: 0

Views: 216

Answers (1)

csabol
csabol

Reputation: 56

Have you looked at CATiledLayer? Its documentation is not very good but offers a lot of performance improvement for drawing a screen full of many small images. There's some sample code from old WWDC's and you might want to check out this tutorial http://www.cimgf.com/2011/03/01/subduing-catiledlayer/

Upvotes: 1

Related Questions