使用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);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.