zoukankan      html  css  js  c++  java
  • CGBitmapContextCreate函数参数详解 以及在 ios7下变化

    函数原型:

    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值决定了绘制像素的透明性。


    CGBitmapContextCreate 在 ios7下变化

    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,不用麻烦的各种写代码。也可以直接进行类型强制转换,这个你随便。只是每个人的习惯不一样,故,如何解决,自己参考决定 。


  • 相关阅读:
    Oracle数据库内置函数
    软件测试工程师笔试题带答案
    JavaBean对象与Map对象互相转化
    Android之MVP模式实现登录和网络数据加载
    AndroidStudio使用偷懒插件Butterknife和GsonFormat
    最强 Android Studio 使用小技巧和快捷键
    android--------Retrofit+RxJava的使用
    Android之封装好的异步网络请求框架
    Android之MVC模式的使用
    Andriod的Http请求获取Cookie信息并同步保存,使第二次不用登录也可查看个人信息
  • 原文地址:https://www.cnblogs.com/songfeixiang/p/3733648.html
Copyright © 2011-2022 走看看