views:

240

answers:

1

Hello, i have a question about the CGLayerRef "elements" ( because they are not object !)

In my app, i want to store a list of CGLayerRef created from bitmaps, for obtain a good framerate on my DrawRect.

how can i store and read the CGLayerRef in this code ?

This is the code for store in the NSMutableArray

    CGLayerRef imageLayer;
// create my layer...
// init  my NSMutableArray
if (layerTable == nil) {
 layerTable = [[NSMutableArray alloc] init];
}
// and store my CGCLayer in this array with NSValue
[layerTable addObject:[[NSValue alloc] initWithBytes:imageLayer objCType:"CGLayerRef"]];

Now the code for DrawRect in my view :

"actualIdp" is a int for identify the layer to draw.

-(void)drawInContext:(CGContextRef)context{
// other code...

// here I want to read my CGLayerRef


//test with NSValue, but i don't know NSValue

NSValue *val = [layerTable objectAtIndex:actualIdp];
CGLayerRef layerToShow;
[val getValue:layerToShow]; // maybe an error

CGContextDrawLayerInRect(context, imageRect, layerToShow );

}

Here, the code for change my id :

-(void)showThisId:(int)idp{
actualIdp = idp;
[self setNeedsDisplay];

};
+2  A: 

You left out the @encode directive:

[layerTable addObject:[[NSValue alloc] initWithBytes:imageLayer objCType:"CGLayerRef"]];

should read:

[layerTable addObject:[[NSValue alloc] initWithBytes:imageLayer objCType:@encode(CGLayerRef)]];

You need to pass the address of a buffer to [NSValue getValue:]. So this line:

[val getValue:layerToShow];

Should be:

[val getValue:&layerToShow];

I think that should work.

TechZen
This answer is good but for best working with my CGCLayerRef value, i have write this line :[layerTable addObject:[[NSValue alloc] initWithBytes:
Pixman