iPhone CALayer图像数组内容值

5 iphone core-animation calayer cgimage ios

将图像加载到图层的方法就是这样:

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

layer.contents = (id) [UIImage imageNamed:@"image.png"].CGImage;
Run Code Online (Sandbox Code Playgroud)

然后将图层作为子图层添加到视图中,如:

假设你在视图中

[self.layer addSublayer:layer];
Run Code Online (Sandbox Code Playgroud)

现在我想加载一个图像数组作为动画,所以最终我会得到动画的图像.

所以在实际执行动画之前,我测试了以下内容:

[values insertObject:(id)[UIImage imageNamed:path].CGImage atIndex:i];
Run Code Online (Sandbox Code Playgroud)

当然有一个循环运行,将每个图像输入到正确的索引...然后我得到一个CGImage..的动画数组.

我打印过这个数组并看到了这个:

CGImage 0x17d900

CGImage 0x17f4e0

所以价值在那里..我没有得到任何错误..但我没有看到图像......

如果你有想法,请告诉我....

Cos*_*que 5

这是一个代码片段,适用于我的一个项目:

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath: @"contents"];
animation.calculationMode = kCAAnimationDiscrete;
animation.duration = 1.0;
animation.values = values; // NSArray of CGImageRefs
[layer addAnimation: animation forKey: @"contents"];
Run Code Online (Sandbox Code Playgroud)

但是,我对动画帧以及旧的iPhone/iPod上的图像造成了严重的性能问题.如果碰到这个,秘密成分是使用预渲染图像(IIRC,它们用私人CABackingStore类表示).简而言之,您创建了一个正确大小的CALayer,drawInContext:用于绘制单个动画帧,然后循环遍历动画帧,在此处为图层提供帧图像,发送它display并将其contents属性保存到数组中.只要您不尝试以任何方式操作预渲染图像,缓存技术就是安全的:基本上,您只是这样做layer1.contents = layer2.contents.

除非遇到性能问题,否则不要浪费时间实现上述操作.