简体   繁体   中英

iPhone CALayer image array contents values

The way to load image to the layer is simply this:

CALayer *layer = [[CALayer alloc]init];

layer.contents = (id) [UIImage imageNamed:@"image.png"].CGImage;

then you add the layer as sublayer to the view something like:

assume you in the view

[self.layer addSublayer:layer];

Now I want to load an array of image as animation so eventually I will get the images animated.

so before actually perform the animation I have tested the following:

[values insertObject:(id)[UIImage imageNamed:path].CGImage atIndex:i];

of course there is a loop that that runs that enter each image to the right index... and then I am getting an array of CGImage .. for the animation.

I have printed this array and saw this:

CGImage 0x17d900

CGImage 0x17f4e0

So the values are there.. and I am not getting any errors .. but I do not see the images...

Let me know if you have an idea ....

This is a code snippet which worked fine for one of my projects:

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath: @"contents"];
animation.calculationMode = kCAAnimationDiscrete;
animation.duration = 1.0;
animation.values = values; // NSArray of CGImageRefs
[layer addAnimation: animation forKey: @"contents"];

However, I had largish images for animation frames and on old iPhones/iPods that caused serious performance problems. If you run into this, the secret ingredient is to use the pre-rendered images (IIRC, they are represented with a private CABackingStore class). In a nutshell, you make a CALayer of the correct size, which uses drawInContext: to draw a single animation frame, then you loop through the animation frames, where you feed the layer a frame image, send it display and save its contents property into an array. The caching technique is safe as long as you don't try to manipulate the pre-rendered images in any way: basically, you simply do layer1.contents = layer2.contents .

Just don't waste your time implementing the above unless you do have performance problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM