CGBitmapContextCreate函数

CGBitmapContextCreate函数参数详解

函数原型:

CGContextRef CGBitmapContextCreate (

   void *data,
   size_t width,
   size_t height,
   size_t bitsPerComponent,
   size_t bytesPerRow,
   CGColorSpaceRef colorspace,
   CGBitmapInfo bitmapInfo

);

参数:

data                                    指向要渲染的绘制内存的地址。这个内存块的大小至少是(bytesPerRow*height)个字节

width                                  bitmap的宽度,单位为像素

height                                bitmap的高度,单位为像素

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

bytesPerRow                  bitmap的每一行在内存所占的比特数

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

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

描述:

当你调用这个函数的时候,Quartz创建一个位图绘制环境,也就是位图上下文。当你向上下文中绘制信息时,Quartz把你要绘制的信息作为位图数据绘制到指定的内存块。一个新的位图上下文的像素格式由三个参数决定:每个组件的位数,颜色空间,alpha选项。alpha值决定了绘制像素的透明性。

Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGBitmapInfo' (aka 'enum CGBitmapInfo')
在使用xcode5 sdk iOS7环境,创建图形上下文进行图形绘制,合并,裁剪,特效处理等时避免不了使用如下方法创建位图:
在 iOS7以前,是使用如下方法创建的:
CG_EXTERN CGContextRef CGBitmapContextCreate(void *data, size_t width,
  size_t height, size_t bitsPerComponent, size_t bytesPerRow,
  CGColorSpaceRef space,CGImageAlphaInfo bitmapInfo)

注意最后一个参数类型是 CGImageAlphaInfo 枚举类型中的kCGImageAlphaPremultipliedLast值。其整型值为1。
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 */
};


但是在iOS7版本中,这个最后的参会类型发生了变化。看一下定义:
CGContextRef CGBitmapContextCreate(void *data, size_t width,
  size_t height, size_t bitsPerComponent, size_t bytesPerRow,
  CGColorSpaceRef space, CGBitmapInfo bitmapInfo)
很明显最后一个参数由CGImageAlphaInfo 变化为 CGBitmapInfo,看一下这个类型的定义
typedef CF_OPTIONS(uint32_t, CGBitmapInfo)
 {
  kCGBitmapAlphaInfoMask = 0x1F,
  kCGBitmapFloatComponents = (1 << 8),
  kCGBitmapByteOrderMask = 0x7000,
  kCGBitmapByteOrderDefault = (0 << 12),
  kCGBitmapByteOrder16Little = (1 << 12),
  kCGBitmapByteOrder32Little = (2 << 12),
  kCGBitmapByteOrder16Big = (3 << 12),
  kCGBitmapByteOrder32Big = (4 << 12)

} CF_ENUM_AVAILABLE(10_4, 2_0);
从头到尾没有发现值为1的枚举量值。故在使用的时候会出现如下警告:

Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGBitmapInfo' (aka 'enum CGBitmapInfo')

意思很明显不过,类型不匹配非法。
以下给出解决方法:
第一种方法,定义宏:
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
     #define kCGImageAlphaPremultipliedLast  (kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast)
#else 
     #define kCGImageAlphaPremultipliedLast  kCGImageAlphaPremultipliedLast
#endif

这样就会直接映射出一个值为1的宏,原有方法不用改变。

第二种方法:原理和第一个一样,目的 还是为了生产出一个为1的值,直接修改代码。
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
    int bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
#else
     int bitmapInfo = kCGImageAlphaPremultipliedLast;
#endif

    CGContextRef context = CGBitmapContextCreate(nil, CGContexWith*2, 290.0*2, 8, 4*CGContexWith*2, colorSpace, bitmapInfo);

其实所有的做法,不外乎为了使这里的值为1,类型匹配。你也直接可以传1,不用麻烦的各种写代码。也可以直接进行类型强制转换,这个你随便。只是每个人的习惯不一样,故,如何解决,自己参考决定 。

转载自:http://hi.baidu.com/yunhuaikong/item/0fe2ccca5f041c7289ad9eac


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C 语言函数,用于在 Apple Watch 上实现网格缩放: ```c void resizeImage(int *image, int width, int height, int newWidth, int newHeight) { int xRatio = (int)((width << 16) / newWidth) + 1; int yRatio = (int)((height << 16) / newHeight) + 1; int *buffer = (int*) malloc(newWidth * newHeight * sizeof(int)); for (int y = 0; y < newHeight; y++) { for (int x = 0; x < newWidth; x++) { int x2 = ((x * xRatio) >> 16); int y2 = ((y * yRatio) >> 16); buffer[(y * newWidth) + x] = image[(y2 * width) + x2]; } } memcpy(image, buffer, newWidth * newHeight * sizeof(int)); free(buffer); } ``` 这个函数接受一个指向图像像素数组的指针,以及原始图像的宽度和高度。它还接受新的宽度和高度,用于缩放图像。该函数使用双线性插值算法将原始图像缩放到新的宽度和高度。 在调用此函数之前,需要将图像加载到像素数组中。可以使用 Apple Watch 上的 Core Graphics 框架来实现此操作。例如,以下是一个简单的函数,用于从 PNG 文件加载图像: ```c int* loadImageFromPNG(char* filename, int* width, int* height) { NSData* data = [NSData dataWithContentsOfFile:[NSString stringWithUTF8String:filename]]; UIImage* image = [UIImage imageWithData:data]; CGImageRef cgImage = [image CGImage]; *width = (int) CGImageGetWidth(cgImage); *height = (int) CGImageGetHeight(cgImage); int bitsPerPixel = 32; int bytesPerPixel = bitsPerPixel / 8; int bytesPerRow = bytesPerPixel * *width; int* imageData = (int*) malloc(*width * *height * sizeof(int)); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(imageData, *width, *height, bitsPerPixel, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGContextDrawImage(context, CGRectMake(0, 0, *width, *height), cgImage); CGContextRelease(context); CGColorSpaceRelease(colorSpace); return imageData; } ``` 这个函数接受一个 PNG 文件名,并返回一个指向像素数组的指针。它还输出图像的宽度和高度。可以使用此函数将 PNG 文件加载到像素数组中,然后使用上面的 `resizeImage` 函数进行缩放。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值