iPhone CALayer 图像数组内容值
将图像加载到图层的方法很简单:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一段代码片段,对于我的一个项目来说效果很好:
但是,我的动画帧和旧 iPhone/iPod 上的图像较大,这导致了严重的性能问题。如果您遇到这种情况,秘诀就是使用预渲染图像(IIRC,它们用私有
CABackingStore
类表示)。简而言之,您创建一个正确大小的 CALayer,它使用drawInContext:
绘制单个动画帧,然后循环遍历动画帧,在其中向图层提供帧图像,将其发送display
并将其内容属性保存到数组中。只要您不尝试以任何方式操作预渲染图像,缓存技术就是安全的:基本上,您只需执行layer1.contents = layer2.contents
即可。除非确实存在性能问题,否则不要浪费时间实现上述内容。
This is a code snippet which worked fine for one of my projects:
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 usesdrawInContext:
to draw a single animation frame, then you loop through the animation frames, where you feed the layer a frame image, send itdisplay
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 dolayer1.contents = layer2.contents
.Just don't waste your time implementing the above unless you do have performance problems.