gpt4 book ai didi

objective-c - CGContextDrawImage 的优化替代品

转载 作者:太空狗 更新时间:2023-10-30 03:54:45 32 4
gpt4 key购买 nike

我目前在 OSX 上使用 CoreGraphics 进行了大量工作。

我对我的代码运行了 Time Profiler,发现最大的问题在于 CGContextDrawImage。它是每秒被调用多次的循环的一部分。

我没有任何方法可以优化此代码本身(因为它在 Apple 库中)- 但我想知道是否有更快的替代方法或提高速度的方法。

我在一些混合模式代码之后使用 CGContextDraw 图像,例如:CGContextSetBlendMode(context, kCGBlendModeDifference); 因此替代实现需要能够支持混合。

时间分析器结果:

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

更新:

实际来源大致如下:

/////////////////////////////////////////////////////////////////////////////////////////
- (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;
}

最佳答案

@JeremyRoman et al: Thank you so much for your comments. I am drawing the same image a couple of times per loop, onto different contexts with different filters, and combining with new images. Does resampling include switching from RGB to RGBA? What could I try to speed up or eliminate resampling? – Chris Nolet

这就是 Core Image 的用途。查看Core Image Programming Guide了解详情。 CGContext 旨在将最终图像渲染到屏幕上,听起来这并不是您创建的每张图像的目标。

关于objective-c - CGContextDrawImage 的优化替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17787822/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com