在 CGImageRef 和 UIImage 之间共享内存

发布于 2024-08-06 10:00:05 字数 892 浏览 2 评论 0原文

有没有办法从屏幕外表面创建 UIImage 对象而不复制底层像素数据?

我想做这样的事情:

// These numbers are made up obviously...
CGContextRef offscreenContext = MyCreateBitmapContext(width, height);

// Draw into the offscreen buffer
// Draw commands not relevant...

// Convert offscreen into CGImage
// This consumes ~15MB
CGImageRef offscreenContextImage = CGBitmapContextCreateImage(offscreenContext);

// This allocates another ~15MB
// Is there any way to share the bits from the 
// CGImageRef instead of copying the data???
UIImage * newImage = [[UIImage alloc] initWithCGImage:offscreenContextImage];

// Releases the original 15MB, but the spike of 30MB total kills the app.
CGImageRelease(offscreenContextImage);
CGContextRelease(offscreenContext);

内存被释放并稳定在可接受的大小,但 30MB 内存峰值会杀死应用程序。有没有办法共享像素数据?

我考虑过将离屏缓冲区保存到文件中并再次加载数据,但这是一个 hack,iPhone 的便捷方法需要 UIImage 来保存它......

Is there any way to create a UIImage object from an offscreen surface without copying the underlying pixel data?

I would like to do something like this:

// These numbers are made up obviously...
CGContextRef offscreenContext = MyCreateBitmapContext(width, height);

// Draw into the offscreen buffer
// Draw commands not relevant...

// Convert offscreen into CGImage
// This consumes ~15MB
CGImageRef offscreenContextImage = CGBitmapContextCreateImage(offscreenContext);

// This allocates another ~15MB
// Is there any way to share the bits from the 
// CGImageRef instead of copying the data???
UIImage * newImage = [[UIImage alloc] initWithCGImage:offscreenContextImage];

// Releases the original 15MB, but the spike of 30MB total kills the app.
CGImageRelease(offscreenContextImage);
CGContextRelease(offscreenContext);

The memory is released and levels out at the acceptable size, but the 30MB memory spike is what kills the application. Is there any way to share the pixel data?

I've considered saving the offscreen buffer to a file and loading the data again, but this is a hack and the convenience methods for the iPhone require a UIImage to save it...

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

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

发布评论

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

评论(2

千秋岁 2024-08-13 10:00:05

您可以尝试在创建 CGImage 后立即释放上下文,释放上下文使用的内存,因为 CGBitmapContextCreateImage() 创建上下文的副本。

像这样:

CGImageRef offscreenContextImage = CGBitmapContextCreateImage(offscreenContext);

CGContextRelease(offscreenContext);

UIImage * newImage = [[UIImage alloc] initWithCGImage:offscreenContextImage];

// ...

CGImageRelease(offscreenContextImage);

You could try releasing the context right after you created the CGImage, releasing the memory used by the context, because CGBitmapContextCreateImage() creates a copy of the context.

Like this:

CGImageRef offscreenContextImage = CGBitmapContextCreateImage(offscreenContext);

CGContextRelease(offscreenContext);

UIImage * newImage = [[UIImage alloc] initWithCGImage:offscreenContextImage];

// ...

CGImageRelease(offscreenContextImage);
芯好空 2024-08-13 10:00:05

或许

UIImage *newImage = [[UIImage alloc[ initWithData:offScreenContext];

Maybe

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