美文网首页
CGBitmapContextCreate(创建位图绘制上下文)

CGBitmapContextCreate(创建位图绘制上下文)

作者: 开发者老岳 | 来源:发表于2020-01-07 12:07 被阅读0次
CG_EXTERN CGContextRef CGBitmapContextCreate 
(
    void *data,
    size_t width, 
    size_t height, 
    size_t bitsPerComponent, 
    size_t bytesPerRow, 
    CGColorSpaceRef space, 
    uint32_t bitmapInfo
);

参数:

  • data:指向要渲染绘制的内存地址。这个内存块大小至少为(bytesPerRow*height)个字节。使用时可传NULL,系统会自动分配内存。
  • width:所需位图(bitmap)的宽度,单位为像素。
  • height:所需位图(bitmap)的高度,单位为像素。
  • bitsPerComponent:内存中像素分量的位数。如,对于32位像素格式和RGB颜色空间时,该值赢设为8。有关支持的像素格式,参阅 Graphics Contexts 中的 Quartz 2D Programming Guide.
  • bytesPerRow:位图每行内存所占的字节数,一个像素一个byte。若前面参数data传的是NULL,则该参数传入0来自动计算。
  • spacebitmap上下文用的颜色空间。注意,位图上下文不支持颜色空间索引。。
  • bitmapInfo:用于指定位图是否包含Alpha通道、Alpha通道在像素中的相对位置、像素分量是整型还是浮点型等信息的常量。想要了解如何指定颜色空间,像素字节数,像素分量字节数,参考 Graphics Contexts

当你绘制上下文的时候,Core Graohics会将你的绘制在指定的内存块中渲染成位图数据。新位图像素格式决定因素有三个:每个分量的位数、颜色空间、Alpha选项(表示为CGBitmapInfo常量)。

typedef CF_OPTIONS(uint32_t, CGBitmapInfo) 
{
    kCGBitmapAlphaInfoMask = 0x1F,

    kCGBitmapFloatInfoMask = 0xF00,
    kCGBitmapFloatComponents = (1 << 8),

    kCGBitmapByteOrderMask     = kCGImageByteOrderMask,
    kCGBitmapByteOrderDefault  = kCGImageByteOrderDefault,
    kCGBitmapByteOrder16Little = kCGImageByteOrder16Little,
    kCGBitmapByteOrder32Little = kCGImageByteOrder32Little,
    kCGBitmapByteOrder16Big    = kCGImageByteOrder16Big,
    kCGBitmapByteOrder32Big    = kCGImageByteOrder32Big
} CG_AVAILABLE_STARTING(10.0, 2.0);
  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
);
  1. 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
);
  1. 其他例子:
    // 这个imageRef并不包含图片的数据
    // 注意:载入的图片大小必须是2的N次方
    CGImageRef imageRef = [[UIImage imageNamed:@"tile_floor.png"] CGImage];
    size_t width = CGImageGetWidth(imageRef);
    size_t height = CGImageGetHeight(imageRef);
        
    //RGBA个数, 每个像素4个位元组
    GLubyte *textureData = (GLubyte *)malloc(width * height * 4);
    CGContextRef textrureContext = CGBitmapContextCreate(
         textureData,
         width,
         height,
         8, // 每个通道8位, CGImageGetBitsPerComponent(source),
         width * 4,
         CGImageGetColorSpace(imageRef), //CGColorSpaceCreateDeviceRGB()
         kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big //CGImageGetBitmapInfo(source)
    );
    CGContextDrawImage(textrureContext, CGRectMake(0, 0, width, height), imageRef);

相关文章

  • CGBitmapContextCreate(创建位图绘制上下文)

    参数:data:指向要渲染绘制的内存地址。这个内存块大小至少为(bytesPerRow*height)个字节。使用...

  • CoreGraphics学习

    官方介绍 CGBitmapContextCreate 创建位图上下文 当你调用这个函数的时候,Quartz创建一个...

  • CGBitmapContext介绍

    使用CGBitmapContextCreate创建绘制图片的上下文 参数data指向绘图操作被渲染的内存区域,这个...

  • IOS 中 给PDF 绘制内容

    绘制到PDF则要启用pdf图形上下文,PDF图形上下文的创建使用方式跟位图图形上下文是类似的,需要注意的一点就是绘...

  • 【译】Creating a Bitmap Graphics Co

    位图图形上下文接受指向包含位图存储空间的内存缓冲区的指针。当您绘制位图图形上下文时,缓冲区会更新。在您释放图形上下...

  • Android中如何将字符串转为Bitmap

    过程 计算字符串绘制成图片时的图片大小 创建位图 绘制位图 代码 示例 将货币符号转成位图显示在控件左侧,将金额作...

  • 位图

    画图片水印开启一个位图上下文,和view无关,所以不需要在drawRect方法中。位图上下文需要我们手动创建,最后...

  • iOS 自己水印图片

    1、创建一个背景图片UIImage对象 2、创建位图上下文 UIGraphicsBeginImageContext...

  • UIGraphicsBeginImageContext系列知识

    UIGraphicsBeginImageContext创建一个基于位图的上下文(context),并将其设置为当前...

  • UIGraphics

    UIGraphicsBeginImageContext 创建一个基于位图的上下文(context),并将其设置为当...

网友评论

      本文标题:CGBitmapContextCreate(创建位图绘制上下文)

      本文链接:https://www.haomeiwen.com/subject/ghjractx.html