2009-12-01 6 views
1

j'ai une question sur les CGLayerRef « éléments » (parce qu'ils ne sont pas objet!)Comment enregistrer et lire un CGLayerRef, dans un NSMutableArray

Dans mon application, je veux stocker une liste de CGLayerRef créé à partir bitmaps, pour obtenir un bon framerate sur mon DrawRect.

comment puis-je stocker et lire le CGLayerRef dans ce code?

C'est le code pour le magasin dans le 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"]]; 

Maintenant, le code pour drawRect à mon avis:

« actualIdp » est un entier pour identifier la couche pour dessiner.

-(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); 

} 

Ici, le code pour changer mon code:

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

}; 

Répondre

2

Vous avez quitté la directive @encode:

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

devrait lire:

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

Vous devez passer l'adresse d'un tampon à [NSValue getValue:]. Donc, cette ligne:

[val getValue:layerToShow]; 

devrait être:

[val getValue:&layerToShow]; 

Je pense que cela devrait fonctionner.

+2

Cette réponse est bonne mais pour mieux travailler avec ma valeur CGCLayerRef, j'ai écrit cette ligne: [layerTable addObject: [[NSValue alloc] initWithBytes: & imageLayer objCType: @encode (CGLayerRef)]]; – Pixman

Questions connexes