首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGContextDrawImage的优化替代方案

CGContextDrawImage的优化替代方案
EN

Stack Overflow用户
提问于 2013-07-22 12:39:50
回答 1查看 2.7K关注 0票数 5

我目前在OSX上大量使用CoreGraphics。

我已经运行了我的代码的时间分析器,并发现最大的挂起是在CGContextDrawImage。它是每秒被多次调用的循环的一部分。

我没有任何方法来优化这段代码本身(因为它在苹果库中),但我想知道是否有更快的替代方案或提高速度的方法。

在一些混合模式代码(如: CGContextDraw )之后,我使用了CGContextSetBlendMode(context, kCGBlendModeDifference);映像,这样替代实现就需要能够支持混合。

时间分析器结果:

代码语言:javascript
复制
3658.0ms   15.0%    0.0               CGContextDrawImage
3658.0ms   15.0%    0.0                ripc_DrawImage
3539.0ms   14.5%    0.0                 ripc_AcquireImage
3539.0ms   14.5%    0.0                  CGSImageDataLock
3539.0ms   14.5%    1.0                   img_data_lock
3465.0ms   14.2%    0.0                    img_interpolate_read
2308.0ms    9.4%    7.0                     resample_band
1932.0ms    7.9%    1932.0                   resample_byte_h_3cpp_vector
369.0ms     1.5%    369.0                    resample_byte_v_Ncpp_vector
1157.0ms    4.7%    2.0                     img_decode_read
1150.0ms    4.7%    8.0                      decode_data
863.0ms     3.5%    863.0                     decode_swap
267.0ms     1.0%    267.0                     decode_byte_8bpc_3

更新:

实际来源大致如下:

代码语言:javascript
复制
/////////////////////////////////////////////////////////////////////////////////////////
- (CGImageRef)createBlendedImage:(CGImageRef)image
                     secondImage:(CGImageRef)secondImage
                       blendMode:(CGBlendMode)blendMode
{
    // Get the image width and height
    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);

    // Set the frame
    CGRect frame = CGRectMake(0, 0, width, height);

    // Create context with alpha channel
    CGContextRef context = CGBitmapContextCreate(NULL,
                                                 width,
                                                 height,
                                                 CGImageGetBitsPerComponent(image),
                                                 CGImageGetBytesPerRow(image),
                                                 CGImageGetColorSpace(image),
                                                 kCGImageAlphaPremultipliedLast);

    if (!context) {
        return nil;
    }

    // Draw the image inside the context
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    CGContextDrawImage(context, frame, image);

    // Set the blend mode and draw the second image
    CGContextSetBlendMode(context, blendMode);
    CGContextDrawImage(context, frame, secondImage);

    // Get the masked image from the context
    CGImageRef blendedImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);

    return blendedImage;
}

/////////////////////////////////////////////////////////////////////////////////////////
- (CGImageRef)createImageTick
{
    // `self.image` and `self.previousImage` are two instance properties (CGImageRefs)

    // Create blended image (stage one)
    CGImageRef stageOne = [self createBlendedImage:self.image
                                       secondImage:self.previousImage
                                         blendMode:kCGBlendModeXOR];

    // Create blended image (stage two) if stage one image is 50% red
    CGImageRef stageTwo = nil;

    if ([self isImageRed:stageOne]) {
        stageTwo = [self createBlendedImage:self.image
                                secondImage:stageOne
                                  blendMode:kCGBlendModeSourceAtop];
    }

    // Release intermediate image
    CGImageRelease(stageOne);

    return stageTwo;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-22 21:07:48

@JeremyRoman等人:非常感谢您的评论。我每循环绘制几次相同的图像,用不同的过滤器绘制不同的上下文,并与新的图像相结合。重采样是否包括从RGB切换到RGBA?我能试着加快或消除重采样吗?-克里斯·诺莱特

这就是核心形象的意义所在。有关细节,请参阅核心图像编程指南CGContext是为将最终图像呈现到屏幕上而设计的,这听起来并不是您创建的每一幅图像的目标。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17787822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档