创建位图上下文

2018-03-13  本文已影响0人  hello_iOS程序媛

在绘制和创建位图中:我们描述了图片的绘制和生成:除了直接使用标准视图展示图片外,有两种情况需要做额外的工作:1)图片为自定义图片的一部分,需要在自定义图片的drawRect:方法中绘制图片 2)离屏渲染图片,使用位图上下文生成一个图片。
在绘制和创建图像中介绍了这两种方法。在创建图像中主要是使用UIGraphicsBeginImageContextWithOptions生成图像上下文,具体可以参考绘制和创建图像。如果你更倾向于使用Core Graphics绘制在位图上下文进行绘制, 可以使用 CGBitmapContextCreate方法来生成上下文和在该上下文中绘制图片内容, 完成绘制后使用CGBitmapContextCreateImage获取CGImageRef对象。
这里将介绍CGBitmapContextCreate相关的方法, CGBitmapContextCreate方法位于CGBitmapContext文件中,该文件中所有方法列表如下图所示。

// CGBitmapContext
/* The callback for releasing the data supplied to
   `CGBitmapContextCreateWithData'. */

typedef void (*CGBitmapContextReleaseDataCallback)(void * __nullable releaseInfo,
    void * __nullable data);

/* Create a bitmap context. The context draws into a bitmap which is `width'
   pixels wide and `height' pixels high. The number of components for each
   pixel is specified by `space', which may also specify a destination color
   profile. The number of bits for each component of a pixel is specified by
   `bitsPerComponent'. The number of bytes per pixel is equal to
   `(bitsPerComponent * number of components + 7)/8'. Each row of the bitmap
   consists of `bytesPerRow' bytes, which must be at least `width * bytes
   per pixel' bytes; in addition, `bytesPerRow' must be an integer multiple
   of the number of bytes per pixel. `data', if non-NULL, points to a block
   of memory at least `bytesPerRow * height' bytes. If `data' is NULL, the
   context will allocate the data itself; this data will be freed when the
   context is deallocated. `bitmapInfo' specifies whether the bitmap should
   contain an alpha channel and how it's to be generated, along with whether
   the components are floating-point or integer. If `releaseCallback' is
   non-NULL, it is called when the context is freed with `releaseInfo' and
   `data' as arguments. */

CG_EXTERN CGContextRef __nullable CGBitmapContextCreateWithData(
    void * __nullable data, size_t width, size_t height, size_t bitsPerComponent,
    size_t bytesPerRow, CGColorSpaceRef cg_nullable space, uint32_t bitmapInfo,
    CGBitmapContextReleaseDataCallback __nullable releaseCallback,
    void * __nullable releaseInfo)
    CG_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_4_0);

/* Create a bitmap context. The context draws into a bitmap which is `width' pixels wide and `height' pixels high. The number of components for each pixel is specified by `space', which may also specify a destination color profile. The number of bits for each component of a pixel is specified by `bitsPerComponent'. The number of bytes per pixel is equal to  `(bitsPerComponent * number of components + 7)/8'. Each row of the bitmap consists of `bytesPerRow' bytes, which must be at least `width * bytes per pixel' bytes; in addition, `bytesPerRow' must be an integer multiple of the number of bytes per pixel. `data', if non-NULL, points to a block of memory at least `bytesPerRow * height' bytes. If `data' is NULL, the data for context is allocated automatically and freed when the context is deallocated. `bitmapInfo' specifies whether the bitmap should contain an alpha channel and how it's to be generated, along with whether the components are floating-point or integer. */
CG_EXTERN CGContextRef __nullable CGBitmapContextCreate(void * __nullable data,
    size_t width, size_t height, size_t bitsPerComponent, size_t bytesPerRow,
    CGColorSpaceRef cg_nullable space, uint32_t bitmapInfo)
    CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/* Return the data associated with the bitmap context `context', or NULL if
   `context' is not a bitmap context. */

CG_EXTERN void * __nullable CGBitmapContextGetData(CGContextRef cg_nullable context)
    CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);

/* Return the width of the bitmap context `context', or 0 if `context' is
   not a bitmap context. */

CG_EXTERN size_t CGBitmapContextGetWidth(CGContextRef cg_nullable context)
    CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);

/* Return the height of the bitmap context `context', or 0 if `context' is
   not a bitmap context. */

CG_EXTERN size_t CGBitmapContextGetHeight(CGContextRef cg_nullable context)
    CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);

/* Return the bits per component of the bitmap context `context', or 0 if
   `context' is not a bitmap context. */

CG_EXTERN size_t CGBitmapContextGetBitsPerComponent(CGContextRef cg_nullable context)
    CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);

/* Return the bits per pixel of the bitmap context `context', or 0 if
   `context' is not a bitmap context. */

CG_EXTERN size_t CGBitmapContextGetBitsPerPixel(CGContextRef cg_nullable context)
    CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);

/* Return the bytes per row of the bitmap context `context', or 0 if
   `context' is not a bitmap context. */

CG_EXTERN size_t CGBitmapContextGetBytesPerRow(CGContextRef cg_nullable context)
    CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);

/* Return the color space of the bitmap context `context', or NULL if
   `context' is not a bitmap context. */

CG_EXTERN CGColorSpaceRef __nullable CGBitmapContextGetColorSpace(
    CGContextRef cg_nullable context)
    CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);

/* Return the alpha info of the bitmap context `context', or
   "kCGImageAlphaNone" if `context' is not a bitmap context. */

CG_EXTERN CGImageAlphaInfo CGBitmapContextGetAlphaInfo(
    CGContextRef cg_nullable context)
    CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);

/* Return the bitmap info of the bitmap context `context', or 0 if `context'
   is not a bitmap context. */

CG_EXTERN CGBitmapInfo CGBitmapContextGetBitmapInfo(
    CGContextRef cg_nullable context)
    CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);

/* Return an image containing a snapshot of the bitmap context `context'. If
   context is not a bitmap context, or if the image cannot be created for
   any reason, this function returns NULL. This is a "copy" operation ---
   subsequent changes to context will not affect the contents of the
   returned image.

   Note that in some cases the copy will actually follow "copy-on-write"
   semantics, so that the actual physical copy of the bits will only occur
   if the underlying data in the bitmap context is modified. As a
   consequence, you may wish to use the resulting image and release it
   before performing more drawing into the bitmap context; in this way, the
   actual physical copy of the data may be avoided. */

CG_EXTERN CGImageRef __nullable CGBitmapContextCreateImage(
    CGContextRef cg_nullable context)
    CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);

创建位图上下文

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:所需位图的宽度(以像素为单位)。
height:所需位图的高度(以像素为单位)。
bitsPerComponent:内存中每个像素组件使用的位数。例如,对于32位像素格式和RGB颜色空间,您可以指定每个组件8位的值。有关支持的像素格式列表,请参见“Quartz 2D编程指南”的图形上下文章节中的“支持的像素格式”。
bytesPerRow:每行位图使用的内存字节数。如果数据参数为NULL,则传递值0会导致自动计算该值。
colorspace:用于位图上下文的颜色空间。请注意,索引颜色空间不支持位图图形上下文。
bitmapInfo:指定位图是否应包含Alpha通道,Alpha通道在像素中的相对位置以及有关像素组件是浮点还是整数值的信息的常量。指定alpha通道信息的常量使用CGImageAlphaInfo类型声明,但可以安全地传递给此参数。您还可以传递与CGBitmapInfo类型关联的其他常量。
返回结果:新的位图上下文,如果无法创建上下文,则为NULL。 你有责任使用CGContextRelease释放这个对象。*/
CGContextRef CGBitmapContextCreateWithData(void *data, size_t width, size_t height, size_t bitsPerComponent, size_t bytesPerRow, CGColorSpaceRef space, uint32_t bitmapInfo, CGBitmapContextReleaseDataCallback releaseCallback, void *releaseInfo);
// 具有指定回调的生成位图上下文

新位图上下文的像素格式由三个参数确定 - 每个组件的位数,色彩空间和alpha选项(表示为CGBitmapInfo常量)。 alpha值决定了一个像素在绘制时的不透明度。

typedef CF_ENUM(uint32_t, CGImageAlphaInfo) {
    kCGImageAlphaNone,               /* For example, RGB. */
    kCGImageAlphaPremultipliedLast,  /* For example, premultiplied RGBA */
    kCGImageAlphaPremultipliedFirst, /* For example, premultiplied ARGB */
    kCGImageAlphaLast,               /* For example, non-premultiplied RGBA */
    kCGImageAlphaFirst,              /* For example, non-premultiplied ARGB */
    kCGImageAlphaNoneSkipLast,       /* For example, RBGX. */
    kCGImageAlphaNoneSkipFirst,      /* For example, XRGB. */
    kCGImageAlphaOnly                /* No color data, alpha data only */
};
// 指向指定的位图上下文的图像数据的指针,如果上下文不是位图上下文,则为NULL。
void * CGBitmapContextGetData(CGContextRef context);
//返回指定上下文的像素宽度,如果上下文不是位图上下文,则为0。
size_t CGBitmapContextGetWidth(CGContextRef context);
//返回指定上下文的像素高度,如果上下文不是位图上下文,则为0。
size_t CGBitmapContextGetHeight(CGContextRef context);
//返回指定上下文的每个组件的比特数,如果上下文不是位图上下文,则为0。
size_t CGBitmapContextGetBitsPerComponent(CGContextRef context);
//返回指定上下文的每个像素的比特数,如果上下文不是位图上下文,则为0。
size_t CGBitmapContextGetBitsPerPixel(CGContextRef context);
//返回指定上下文的每行字节数,如果上下文不是位图上下文,则为0。
size_t CGBitmapContextGetBytesPerRow(CGContextRef context);
//返回指定上下文的颜色空间,如果上下文不是位图上下文,则为NULL。
CGColorSpaceRef CGBitmapContextGetColorSpace(CGContextRef context);
// 获取与位图图形上下文关联的alpha信息, 如果上下文不是位图上下文,则为kCGImageAlphaNone。
CGImageAlphaInfo CGBitmapContextGetAlphaInfo(CGContextRef context);
// 获取与位图图形上下文关联的位图信息, 如果上下文不是位图上下文,则为NULL。
CGBitmapInfo CGBitmapContextGetBitmapInfo(CGContextRef context);

例如:

    CGSize thumbSize= CGSizeMake(1080, 140);
    int bitmapInfo = kCGImageAlphaPremultipliedLast;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context =  CGContextRef context = CGBitmapContextCreate(NULL,
                                                 thumbSize.width,
                                                 thumbSize.height,
                                                 8,//bits per component
                                                 thumbSize.width*4,
                                                 colorSpace,
                                                 bitmapInfo);
//获取上述代码的图形上下文相关参数
(lldb) po CGBitmapContextGetData(context)
0x0000000111a70020

(lldb) po thumbSize
(width = 1080, height = 140)

(lldb) po CGBitmapContextGetWidth(context)
1080

(lldb) po CGBitmapContextGetHeight(context)
140

(lldb) po CGBitmapContextGetBitsPerComponent(context)
8

(lldb) po CGBitmapContextGetBitsPerPixel(context)
32

(lldb) po CGBitmapContextGetBytesPerRow(context)
4320

(lldb) po CGBitmapContextGetColorSpace(context)
<CGColorSpace 0x17403fdc0> (kCGColorSpaceDeviceRGB)

(lldb) po CGBitmapContextGetAlphaInfo(context)
1

(lldb) po CGBitmapContextGetBitmapInfo(context)
1
上一篇下一篇

猜你喜欢

热点阅读