加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 将CIImage转换为CGImage太慢了

发布时间:2020-12-14 18:09:38 所属栏目:百科 来源:网络整理
导读:我需要将CI Image转换为CG Image.这是我目前使用的代码: CGImageRef img = [myContext createCGImage:ciImage fromRect:[ciImage extent]]; 但是对于具有常规尺寸的图像,他的代码行非常慢.很慢,我可以正确使用它.还有另一种更快的方法将这些CIImages转换为C
我需要将CI Image转换为CG Image.这是我目前使用的代码:

CGImageRef img = [myContext createCGImage:ciImage fromRect:[ciImage extent]];

但是对于具有常规尺寸的图像,他的代码行非常慢.很慢,我可以正确使用它.还有另一种更快的方法将这些CIImages转换为CGImages吗?

最后,我使用Core Graphics将CGImages绘制到CGContext.如果有办法直接将CIImages绘制到CGContext,我认为这也应该更快.这可能吗?

谢谢

解决方法

从 CIImage documentation:

Although a CIImage object has image data associated with it,it is not
an image. You can think of a CIImage object as an image “recipe.” A
CIImage object has all the information necessary to produce an image,
but Core Image doesn’t actually render an image until it is told to do
so. This “lazy evaluation” method allows Core Image to operate as
efficiently as possible.

这意味着,在渲染图像之前,您可能已经应用(或者更好地请求应用)到CIImage的任何过滤都不会实际发生,在您创建CGImageRef时会发生这种情况.这可能是你遇到减速的原因.

也就是说,过滤CIImage通常是一个非常快速的过程,所以根据你的图像大小,你应该给我们你的慢速定义.

最后要确保瓶颈确实在你认为的地方,你应该使用仪器profile your application.

编辑

我刚刚尝试构建一个示例项目,通过应用CIColorMap彩色滤镜在iPad3上过滤来自设备相机(960×540)的图像流,我的速度超过60fps(~16ms).

根据您的应用程序,您可以通过重用CIContext,ColorMapFilter,inputGradientImage(如果它不随时间变化)并在每次迭代时仅更新inputImage来获得更好的性能.

例如,您可以调用prepareFrameFiltering一次,然后在您要处理的每个帧上重复调用applyFilterToFrame :.

@property (nonatomic,strong) CIContext *context;
@property (nonatomic,strong) CIFilter *colorMapFilter;

- (void)prepareFrameFiltering {
    self.context = [CIContext contextWithOptions:nil];
    CIImage *colorMap = [CIImage imageWithCGImage:[UIImage imageNamed:@"gradient.jpg"].CGImage];
    self.colorMapFilter = [CIFilter filterWithName:@"CIColorMap"];
    [self.colorMapFilter setValue:colorMap forKey:@"inputGradientImage"];
}

- (void)applyFilterToFrame:(CIImage *)ciFrame {    
    // filter
    [self.colorMapFilter setValue:ciFrame forKey:@"inputImage"];
    CIImage *ciImageResult = [self.colorMapFilter valueForKey: @"outputImage"];

    CGImageRef ref = [self.context createCGImage:ciImageResult fromRect:ciFrame.extent];

    // do whatever you need

    CGImageRelease(ref);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读