5

将图像加载到图层的方法很简单:

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

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

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

假设你在视图中

[self.layer addSublayer:layer];

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

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

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

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

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

CGImage 0x17d900

CGImage 0x17f4e0

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

如果您有任何想法,请告诉我......

4

1 回答 1

5

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

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

但是,我的动画帧和旧 iPhone/iPod 上的图像较大,这会导致严重的性能问题。如果遇到这种情况,秘诀是使用预渲染图像(IIRC,它们用私有CABackingStore类表示)。简而言之,您制作了一个正确大小的 CALayer,用于drawInContext:绘制单个动画帧,然后循环遍历动画帧,在其中为图层提供帧图像,发送display并将其内容属性保存到数组中。只要您不尝试以任何方式操作预渲染的图像,缓存技术是安全的:基本上,您只需执行layer1.contents = layer2.contents.

除非您确实遇到性能问题,否则不要浪费时间执行上述操作。

于 2011-01-31T19:39:54.103 回答