使用CGBitmapContextCreate创建绘制图片的上下文

函数原型:

CGContextRef CGBitmapContextCreate (

void *data,

size_t width,

size_t height,

size_t bitsPerComponent,

size_t bytesPerRow,

CGColorSpaceRef colorspace,

CGBitmapInfo bitmapInfo

);

参数:

data                                    指向要渲染的绘制内存的地址。这个内存块的大小至少是(bytesPerRow*height)个字节。使用时可填NULL或unsigned char类型的指针。

width                                  bitmap的宽度,单位为像素

height                                bitmap的高度,单位为像素

bitsPerComponent        内存中像素的每个组件的位数.例如,对于32位像素格式和RGB 颜色空间,你应该将这个值设为8。

bytesPerRow                  bitmap的每一行在内存所占的比特数,一个像素一个byte。

colorspace                      bitmap上下文使用的颜色空间。

bitmapInfo                       指定bitmap是否包含alpha通道,像素中alpha通道的相对位置,像素组件是整形还是浮点型等信息的字符串。


1.RGBA:

const CGSize size = size;

const size_t bitsPerComponent = 8;

const size_t bytesPerRow = size.width * 4; 

CGBitmapContextCreate(calloc(sizeof(unsigned char), bytesPerRow * size.height), size.width, size.height,

bitsPerComponent, bytesPerRow,

CGColorSpaceCreateDeviceRGB(),

kCGImageAlphaPremultipliedLast);


2.only alpha

const CGSize size              = size;

const size_t bitsPerComponent  = 8;

const size_t bytesPerRow        = size.width; 

CGContextRef context        =

CGBitmapContextCreate(calloc(sizeof(unsigned char), bytesPerRow * size.height), size.width, size.height,

bitsPerComponent, bytesPerRow,

NULL,

kCGImageAlphaOnly);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容