简体   繁体   English

OSX Xcode:用指定的RGB颜色预填充位图吗?

[英]OSX Xcode: Pre-filling bitmap with a specified RGB color?

Is there a method for filling a bitmap (context) with a specified RGB color? 有没有一种方法可以用指定的RGB颜色填充位图(上下文)?

Here is the procedure that I have implemented: 这是我执行的过程:

  1. allocate memory for bitmap (malloc) 为位图分配内存(malloc)
  2. memset allocated memory with zeros (to get a black background) memset分配的内存为零(以获取黑色背景)
  3. Create CGContextRef: 创建CGContextRef:

    CGContextRef ctx = CGBitmapContextCreate(memData, width, height,8,bytesPerRow,colorSpace,bmpInfo) CGContextRef ctx = CGBitmapContextCreate(memData,width,height,8,bytesPerRow,colorSpace,bmpInfo)

  4. Insert image: 插入图片:

    CGContextDrawImage(ctx, CGRectMake(x,y,imgWidth, imgHeight), anotherImg) CGContextDrawImage(ctx,CGRectMake(x,y,imgWidth,imgHeight),anotherImg)

  5. Finalize image: 完成图片:

    CGImageRef createdImg = CGBitmapContextCreateImage(context) CGImageRef createdImg = CGBitmapContextCreateImage(context)

As you can see from the above, the background will always be black. 从上面可以看到,背景将始终为黑色。 I want to be able to select an RGB color for the background. 我希望能够为背景选择RGB颜色。 How is that done? 怎么做?

This is for an OSX app in XCode. 这适用于XCode中的OSX应用程序。

My functions are implemented in C as I'm not too comfortable with the Objective C syntax. 我的函数在C中实现,因为我不太熟悉Objective C语法。

Set the fill colour ( CGContextSetFillColorWithColor ) and then fill the whole context ( CGContextFillRect ). 设置填充颜色( CGContextSetFillColorWithColor ),然后填充整个上下文( CGContextFillRect )。

Also, you might want to consider passing NULL as the data parameter to CGBitmapContextCreate so that you don't need to worry about the memory management for it. 另外,您可能要考虑将NULL作为data参数传递给CGBitmapContextCreate这样您就不必担心它的内存管理了。

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

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