开发者

iPhone CALayer image array contents values

开发者 https://www.devze.com 2023-02-07 13:45 出处:网络
The way to load image to the layer is simply this: CALayer *layer = [[CALayer alloc]init]; layer.conten开发者_如何学运维ts = (id) [UIImage imageNamed:@\"image.png\"].CGImage;

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

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

layer.conten开发者_如何学运维ts = (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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号