views:

1338

answers:

2

I have created a CG context that is 800 pixels wide and 1200 pixels height. I have created CGLayer over this context that has been transformed (scaled, translated and rotated). So, at some point, as the CGLayer is bigger than the context and has been translated, rotated, etc., not all parts of this CGLayer falls inside the context. See next picture:

layer and context

As you can see by the picture, some parts of the layer falls outside the context area. When I render the final composition using

CGContextDrawLayerInRect(context, superRect, objectLayer);

it will render the full layer, including those unnecessary parts outside the context.

My problem is: if I can make it draw just the relevant parts inside the context I can make it render fast and save memory.

Is there any way to do that?

NOTE: LAYER contains transparency.

Please refrain from giving solutions that don't involve CGLayers.

thanks in advance.

A: 

Can you use a CATiledLayer? This should lazy-load in squares ala google maps....

+(Class)layerClass
{
return [CATiledLayer class];
}


-(id)init {
   CATiledLayer *tiledLayer = (CATiledLayer *) self.layer;
   tiledLayer.tileSize = CGSize(x,x);
   tiledLayer.levelsOfDetail = y;
   tiledLayer.levelsOfDetailBias = z;
}
Mobs
No, unfortunately I have to use CGLayer. One of the reasons is because I have to write offscreen. But anyway I was not able to find a simple code that explains how CATiledLayer works. It seems to be one of those esoteric functions Apple has documented vague as hell.
Digital Robot
@Mike: It's neither esoteric nor a function, it's a CALayer subclass.
Chuck
I have found this example helpful in the past, it draws using PDF functions, but otherwise illustrates the tiledlayer very well.http://www.olivetoast.com/blog/2009/08/simple-uiscrollview-catiledlayer-pdf-example/
Mobs
thanks!!!!!!!!!!!
Digital Robot
+1  A: 

You can clip the context using CGContextClip/-ToMask/-ToRect.

But i think it's actually cheaper/faster to simply 'dump' pixels into a context, than to have to calculate the clipping bounds and 'draw less'.

The surplus drawing doesn't (normally) use-up extra memory.

arri