Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGB

来源:互联网 发布:知乎石油出口禁令 编辑:程序博客网 时间:2024/06/10 18:20

在使用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。


<span style="font-size:14px;">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 */};</span>

但是在iOS7版本中,这个最后的参会类型发生了变化。看一下定义:

CGContextRef CGBitmapContextCreate(void *data,size_t width,

  size_t height,size_t bitsPerComponent,size_t bytesPerRow,

  CGColorSpaceRef space,CGBitmapInfo bitmapInfo)

很明显最后一个参数由CGImageAlphaInfo 变化为 CGBitmapInfo,看一下这个类型的定义


<span style="font-size:14px;">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);</span>

从头到尾没有发现值为1的枚举量值。故在使用的时候会出现如下警告:


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


意思很明显不过,类型不匹配非法。

以下给出解决方法:

第一种方法,定义宏:

<span style="font-size:14px;">#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1     #define kCGImageAlphaPremultipliedLast  (kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast)#else      #define kCGImageAlphaPremultipliedLast  kCGImageAlphaPremultipliedLast#endif</span>

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


第二种方法:原理和第一个一样,目的 还是为了生产出一个为1的值,直接修改代码。

<span style="font-size:14px;">#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);</span>

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

原创粉丝点击