内存映射 UIImage
我有一个 UIImage,我想将其数据放入一个文件中,然后使用映射文件来节省一些内存。显然,UIImage
数据是私有的,无法访问它。您有什么建议来解决这个问题吗?
谢谢!
I have a UIImage
and I would like to put its data in a file and and then used a mapped file to save some memory. Apparently, the UIImage
data is private and it's not possible to access it. Would you have any suggestions to solve that?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要对编码的图像数据进行内存映射,请映射一个文件并通过将 CGDataProviderRef 传递给 CGImageCreate 来提供对数据的引用。
其中 munmap_wrapper 是这样的:
如果您想要内存映射实际像素,而不是编码的源数据,您可以使用 CGBitmapContext 执行类似的操作。您还可以创建提供程序和图像,以便图像引用与上下文相同的像素。上下文中绘制的任何内容都将是图像的内容。上下文和图像的宽度、高度、色彩空间和其他参数应该相同。
在这种情况下,长度至少为 bytes_per_row*height 字节,因此文件必须至少有那么大。
如果您有现有图像并且想要映射像素,请使用图像的大小和颜色空间创建位图上下文,并使用 CGContextDrawImage 在上下文中绘制图像。
您没有说明图像的来源,但如果您在运行时创建它,那么直接在位图上下文中创建它会更有效。任何图像创建都需要在幕后使用位图上下文,因此它也可能从一开始就是内存映射的。
If you want to memory map the encoded image data, then mmap a file and provide a reference to the data by passing a CGDataProviderRef to CGImageCreate.
Where munmap_wrapper is something like this:
If you want to memory map the actual pixels, instead of the encoded source data, you would do something similar with a CGBitmapContext. You would also create the provider and image so the image refers to the same pixels as the context. Whatever is drawn in the context will be the content of the image. The width, height, color space and other parameters should be identical for the context and image.
In this case, length will be at least bytes_per_row*height bytes so the file must be at least that large.
If you have an existing image and you want to mmap the pixels, then create the bitmap context with the size and color space of your image and use CGContextDrawImage to draw the image in the context.
You did not say the source of your image, but if you are creating it at runtime it would be more efficient to create it directly in the bitmap context. Any image creation requires a bitmap context behind the scenes, so it might as well be the memory mapped one from the start.