(iphone) 维护 CGContextRef 或 CGLayerRef 占用的内存比 UIImage 少?
我需要处理许多图像,但无法将所有图像作为 UIImage 保存在内存中,因为它们太大了。
我还需要更改图像的颜色并即时合并它们。
当内存中没有很多图像时,从底层 NSData 创建 UIImage、更改颜色并组合它们是相当慢的。 (据我所知)
我想也许我可以存储底层 CGLayerRef(用于将要组合的图像)和 CGContextRef(生成的组合图像)。
我是绘图世界的新手,不确定 CGLayerRef 或 CGContextRef 的内存是否比 UIImage 本身小。
我最近听说 w*h 图像占用内存 w*h*4 字节。
CGLayerRef 或 CGContextRef 也占用那么多内存吗?
谢谢
- 编辑
以下是我正在考虑的降低内存使用量和计算时间的策略。
CGLayerRef layer = CGLayerCreateWithContext(self.bitmapContext, self.frame.size, NULL);
CGContextRef layerContext = CGLayerGetContext(layer);
CGContextDrawImage(layerContext, self.bounds, image.CGImage);
store 'layer' somewhere.
possibly destroy layerContext
I need to work with many images, and I can't hold all of them as UIImage in memory because they are too big.
I also need to change colors of image and merge them on the fly.
Creating UIImage from underlying NSData, change color, and combine them when you can't have many images on memory is fairly slow. (as far as I can get)
I thought maybe I can store underlying CGLayerRef(for image that will be combined) and CGContextRef(the resulting combined image).
I am new to drawing world, and not sure if CGLayerRef or CGContextRef is smaller in memory than UIImage itself.
I recently heard that w*h image takes up w*h*4 bytes in memory.
Does CGLayerRef or CGContextRef also take up that much memory?
Thank you
- edit
Following is the strategy I'm thinking of to lower memory usage, and computing time.
CGLayerRef layer = CGLayerCreateWithContext(self.bitmapContext, self.frame.size, NULL);
CGContextRef layerContext = CGLayerGetContext(layer);
CGContextDrawImage(layerContext, self.bounds, image.CGImage);
store 'layer' somewhere.
possibly destroy layerContext
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
图像的内存使用量几乎完全被位图占用。无论您使用
UIImage
还是CGImageRef
或者其他什么,这都不会改变。位图仍在内存中。The memory usage of an image is almost entirely taken up in the bitmap. Whether you use
UIImage
orCGImageRef
or something else, that won't change. The bitmap is still in memory.