iPhone CALayer 图像数组内容值

发布于 2024-10-15 01:47:35 字数 656 浏览 7 评论 0原文

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

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

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

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

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 ....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2024-10-22 01:47:35

这是一段代码片段,对于我的一个项目来说效果很好:

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即可。

除非确实存在性能问题,否则不要浪费时间实现上述内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文