内存映射 UIImage

发布于 2024-09-09 23:39:12 字数 114 浏览 5 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(1

又爬满兰若 2024-09-16 23:39:12

如果要对编码的图像数据进行内存映射,请映射一个文件并通过将 CGDataProviderRef 传递给 CGImageCreate 来提供对数据的引用。

mapped = mmap( NULL , length , ... );
provider = CGDataProviderCreateWithData( mapped , mapped , length , munmap_wrapper );
image = CGImageCreate( ... , provider , ... );
uiimage = [UIImage imageWithCGImage:image];
...

其中 munmap_wrapper 是这样的:

// conform to CGDataProviderReleaseDataCallback
void munmap_wrapper( void *p , const void *cp , size_t l ) { munmap( p , l ); }

如果您想要内存映射实际像素,而不是编码的源数据,您可以使用 CGBitmapContext 执行类似的操作。您还可以创建提供程序和图像,以便图像引用与上下文相同的像素。上下文中绘制的任何内容都将是图像的内容。上下文和图像的宽度、高度、色彩空间和其他参数应该相同。

context = CGBitmapContextCreate( mapped , ... );

在这种情况下,长度至少为 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.

mapped = mmap( NULL , length , ... );
provider = CGDataProviderCreateWithData( mapped , mapped , length , munmap_wrapper );
image = CGImageCreate( ... , provider , ... );
uiimage = [UIImage imageWithCGImage:image];
...

Where munmap_wrapper is something like this:

// conform to CGDataProviderReleaseDataCallback
void munmap_wrapper( void *p , const void *cp , size_t l ) { munmap( p , l ); }

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.

context = CGBitmapContextCreate( mapped , ... );

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.

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