有没有办法在不复制基础像素数据的情况下从屏幕外表面创建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的内存峰值会杀死应用程序。有没有办法分享像素数据?
我考虑将屏幕外缓冲区保存到文件并再次加载数据,但这是一个黑客攻击,iPhone的便捷方法需要UIImage才能保存...
答案 0 :(得分:0)
您可以尝试在创建CGImage之后立即释放上下文,释放上下文使用的内存,因为CGBitmapContextCreateImage()会创建上下文的副本。
像这样:
CGImageRef offscreenContextImage = CGBitmapContextCreateImage(offscreenContext);
CGContextRelease(offscreenContext);
UIImage * newImage = [[UIImage alloc] initWithCGImage:offscreenContextImage];
// ...
CGImageRelease(offscreenContextImage);
答案 1 :(得分:-2)
也许
UIImage *newImage = [[UIImage alloc[ initWithData:offScreenContext];