views:

836

answers:

1

I'm probably just missing something really simple but whenever I add CALayers after the view is visible they don't render. Here's the code I'm using:

[[imageLayers objectAtIndex:0] removeFromSuperlayer];
[imageLayers removeObjectAtIndex:0];
[[imageLayers objectAtIndex:0] removeFromSuperlayer];
[imageLayers removeObjectAtIndex:0];
firstImageOffset = (firstImageOffset + 2) % [pieceCache count];
int topIndex = (firstImageOffset + 6) % [pieceCache count];
int bottomIndex = (firstImageOffset + 7) % [pieceCache count];
float xPos = [(CALayer*)[imageLayers lastObject] position].x + kShiftXPixels;

CALayer* layer1 = [CALayer layer];
[layer1 setFrame:CGRectMake(0, 0, kImageWidth, kImageHeight)];
[layer1 setContents:(id)[UIImage imageNamed:[[pieceCache objectAtIndex:topIndex] objectForKey:@"image_name"]]];
[[[self view] layer] addSublayer:layer1];
[layer1 setPosition:CGPointMake(xPos, kTopRowYPos)];
[imageLayers addObject:layer1];

CALayer* layer2 = [CALayer layer];
[layer2 setFrame:CGRectMake(0, 0, kImageWidth, kImageHeight)];
[layer2 setContents:(id)[UIImage imageNamed:[[pieceCache objectAtIndex:bottomIndex] objectForKey:@"image_name"]]];
[[[self view] layer] addSublayer:layer2];
[layer2 setPosition:CGPointMake(xPos, kBottomRowYPos)];
[imageLayers addObject:layer2];

I've also tried reusing the same layers and just setting the contents to a different CGImage (via setContents:), this also causes the layer to stop rendering (more specifically it animates as if the opacity has been set to 0 thought it's still 1). I've verified that the layers are actually in the view's layer tree by enumerating the view layer's sublayers. They appear to have the right images and be in the correct positions, they just don't draw. Any help would be greatly appreciated.

+1  A: 

It turns out this resulted from a typo. In the lines where I'm calling setContents, I'm sending it a UIImage rather than a CGImage. Sending the correct type resulted in the expected behavior.

ayasin