5

我无法从 CIImage 获取 UIImage。下面的代码行在 iOS6 上运行良好:(输出图像是 CIImage)

self.imageView = [UIImage imageWithCIImage:outputImage];

或者

[self.imageView setImage:[UIImage imageWithCIImage:outputImage]];

当我在运行 iOS 5 的设备上运行同一行代码时,imageView 为空白。如果我记录 UIImage 的 size 属性,它是正确的,但图像永远不会显示在屏幕上。

当我使用 CGImageRef(如下所示)时,它在两种设备上都可以正常工作,但是当我进行堆镜头分析时,它会导致巨大的内存增长。

context = [CIContext contextWithOptions:nil];
CGImageRef ref = [context createCGImage:outputImage fromRect:outputImage.extent];
self.imageView.image = [UIImage imageWithCGImage:ref scale:1.0 orientation:UIImageOrientationUp];
CGImageRelease(ref);

有谁知道为什么 UIImage imageWithCIImage 不起作用?根据 UIImage 类参考,它应该适用于 iOS5 及更高版本。另外,为什么使用 CGImageRef 会导致如此巨大的堆增长?

谢谢

4

1 回答 1

4

On iOS 5.0, -imageWithCIImage: never seemed to work properly, and we were told to use the -createCGImage:fromRect: approach you describe above in order to render the Core Image filter chain to a raster output.

As you see, the downside to this is that -createCGImage:fromRect: creates a new CGImageRef at the size of your target image, and the bitmap at its heart is passed on to your new UIImage. This probably means that you have at least two full bitmaps representing your final filtered frame in memory at one point, which could cause quite a spike if these are large images.

It appears that -imageWithCIImage: has been fixed in iOS 6.0 (Core Image has had a lot of improvements made from 5.0 to 6.0). While I can only speculate as to why it doesn't lead to this memory spike, I bet it's due to the use of a texture cache to share memory between the output OpenGL ES texture from the filtering process and the final bitmap that's stored in your UIImage. I do this in my GPUImage framework to cut down on memory consumption when filtering large images, and it would make sense for Core Image to do the same. Again, this is just speculation.

Unfortunately, it looks like you'll need to do a little version testing here and fall back to the old way of getting output from CIImages if you want to support iOS 5.0 with your application.

于 2012-10-15T22:10:10.147 回答