简体   繁体   English

如何为Alpha蒙版图像创建位图上下文?

[英]How to create a bitmap context for an alpha mask image?

I want to draw an alpha mask image in code. 我想在代码中绘制一个Alpha蒙版图像。 Right now I do: 现在,我这样做:

1) Create a bitmap context using CGBitmapContextCreate with options CGColorSpaceCreateDeviceRGB and kCGImageAlphaPremultipliedFirst . 1)创建使用位图上下文CGBitmapContextCreate带有选项CGColorSpaceCreateDeviceRGBkCGImageAlphaPremultipliedFirst

2) Then I draw into this context, using only grayscale colors like white and black. 2)然后,我仅使用诸如白色和黑色之类的灰度颜色来画图。

3) Then I create a mask image from that context, using CGImageMaskCreate . 3)然后,我使用CGImageMaskCreate从该上下文创建一个蒙版图像。

Conclusion: I waste a lot of memory! 结论:我浪费了很多内存! Because from my understanding, a mask image is grayscale only, right? 因为据我了解,蒙版图像仅是灰度的,对吗? So why create a context in ARGB in the first place. 那么,为什么要首先在ARGB中创建上下文。

How can I create a CGContextRef that is intended to be used for drawing a mask image? 如何创建用于绘制蒙版图像的CGContextRef? My thoughts are to use CGColorSpaceCreateDeviceGray , but here the problems start. 我的想法是使用CGColorSpaceCreateDeviceGray ,但是问题从这里开始。 This is the exact code how I create my ARGB bitmap context: 这是我创建ARGB位图上下文的确切代码:

CGContextRef    context = NULL;
CGColorSpaceRef colorSpace;
uint32_t *      bitmapData;

int imageWidth = round(size.width);
int imageHeight = round(size.height);

int bitmapBytesPerRow = (imageWidth * 4);
int bitmapByteCount = (bitmapBytesPerRow * imageHeight);

colorSpace = CGColorSpaceCreateDeviceRGB();

bitmapData = malloc(bitmapByteCount);

context = CGBitmapContextCreate(bitmapData,
                                imageWidth,
                                imageHeight,
                                8,  // bits per component
                                bitmapBytesPerRow,
                                colorSpace,
                                kCGImageAlphaPremultipliedFirst);

CGColorSpaceRelease(colorSpace);

I am not sure how to compute the bitmapBytesPerRow for such a context. 我不确定如何为这样的上下文计算bitmapBytesPerRow I assume it would be just imageWidth ? 我认为那只是imageWidth吗? And what must I supply for bits per component in CGBitmapContextCreate ? CGBitmapContextCreate bits per component我必须提供什么?

There is CGColorSpaceGetNumberOfComponents() but it reports only the number of components. CGColorSpaceGetNumberOfComponents()但它仅报告组件数。 This does not tell me how many bytes a component has. 这不能告诉我组件有多少字节。

Also what makes me nervous is that the 4 and 8 are hard-coded in my code above. 同样让我感到紧张的是48在上面的代码中是硬编码的。 Who says it's always 4 bytes per component, and who says it's 8 bits per component? 谁说每个组件总是4个字节,谁说每个组件8个字节? I just took this from various sample codes out there. 我只是从各种示例代码中获取了这一点。 Everyone seems to do it this way. 每个人似乎都是这样做的。 It works. 有用。 But future proof? 但是未来的证明? Probably not. 可能不是。

You would make my day with some great answers. 您会用一些很棒的答案来度过我的一天。 Thanks. 谢谢。


Edit: I found a code-snippet, but it is confusing: 编辑:我找到了一个代码段,但令人困惑:

CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceGray();
CGContextRef gradientBitmapContext = CGBitmapContextCreate (NULL, 1, reflectRect.size.height,8, 0, colorSpace2, kCGImageAlphaNone);

Why 0 for bytes per row? 为什么每行字节为0? The documentation does not say you can pass 0. Looks wrong. 文档没有说您可以传递0。看起来不对。

Those parameters are telling the system how to treat the data or memory you supply. 这些参数告诉系统如何处理您提供的数据或内存。 You have created that yourself, so you know what layout you intend. 您已经创建了该布局,因此您知道要使用哪种布局。 What, if anything, the system might want to do with it behind the scenes is not your immediate problem. 系统可能希望在后台处理它,如果有的话,这不是您的直接问题。

In this case, you'll provide 8 bits per sample, with just the 1 component, and probably not want to use any row padding, in which case your bytesPerRow should indeed be the same as the image width. 在这种情况下,您将为每个样本提供8位,仅包含1个分量,并且可能不想使用任何行填充,在这种情况下, bytesPerRow确实应该与图像宽度相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM