CGContextRef CIImageRef详解

第一种 
先用UIImage对象加载一张图片 
然后转化成CGImageRef放到CGContext中去编辑

第二种 
用CGImageCreate函数创建CGImageRef 
然后把CGImageRef放到CGContext中去编辑

第三种 
用CGImageCreateCopy 或者 CGImageCreateCopyWithColorSpace 
函数拷贝

CGImageRef CGImageCreate (

    size_t width, //图片的宽度

    size_t height, //图片的高度

    size_t bitsPerComponent, //图片每个颜色的bits,比如rgb颜色空间,有可能是5 或者 8 ==

    size_t bitsPerPixel, //每一个像素占用的buts,15 位24位 32位等等

    size_t bytesPerRow, //每一行占用多少bytes 注意是bytes不是bits  1byte = 8bit

    CGColorSpaceRef colorspace, //颜色空间,比如rgb

    CGBitmapInfo bitmapInfo, //layout ,像素中bit的布局, 是rgba还是 argb,==

    CGDataProviderRef provider, //数据源提供者,url或者内存==

    const CGFloat decode[], //一个解码数组

    bool shouldInterpolate, //抗锯齿参数

    CGColorRenderingIntent intent //图片渲染相关参数

 }

创建 image mask的一个途径 
CGImageMaskCreate

给图片加mask有两种方法, 
第一种 
使用函数 CGImageCreateWithMask 或者 CGImageCreateWithMaskingColors 在图片上直接打上mask 
这样做对原来的图片要求带有 alpha通道,假如没有,那么不会有半透明的效果

第二种 
使用 CGContextClipToMask 在图形上下问的某个矩形区域打上mask,这样做无论原先的图片有没有alpha通道,都可以实现半透明效果

位图永远是矩形形状的

iphone支持图片格式: 
JPEG, GIF, PNG, TIF, ICO, GMP, XBM, and CUR.

创建一张图片,我们需要提供的一些东西 
A bitmap data source 
An optional Decode Array, 这个数组在渲染的时候会应用在每一个像素上面,是一个颜色变成另外一个颜色。 
An interpolation setting, 布尔值,缩放图片的时候是否采用interpolation算法,具体这个算法不怎么了解 
渲染意向 
图片尺寸 
Pixel Format:这个包括3个东西, 
1,Bits per component,每一个component有多少Bits 
2. Bits per pixel, 每一个像素占多少Bits 
3. Bytes per row,每一个占多少Bytes 
Color Spaces and Bitmap Layout,这里需要提供的几个东西 
1,Whether a bitmap contains an alpha channel. 位图是否包含alpha通道 
2。 color components是否已经乘以alpha value 
3。数据格式是浮点数还是整型 
Bitmap Layout是指color components的指是怎么指定的 
下面的参数 bitmapInfo就是 Bitmap Layout

GImageRef CGImageCreate (

    size_t width,

    size_t height,

    size_t bitsPerComponent,

    size_t bitsPerPixel,

    size_t bytesPerRow,

    CGColorSpaceRef colorspace,

    CGBitmapInfo bitmapInfo,

    CGDataProviderRef provider,

    const CGFloat decode[],

    bool shouldInterpolate,

    CGColorRenderingIntent intent

);/// 选择一个函数来创建图像

CGImageRef CGImageCreate (

    size_t width,

    size_t height,

    size_t bitsPerComponent,

    size_t bitsPerPixel,

    size_t bytesPerRow,

    CGColorSpaceRef colorspace,

    CGBitmapInfo bitmapInfo,

    CGDataProviderRef provider,

    const CGFloat decode[],

    bool shouldInterpolate,

    CGColorRenderingIntent intent

);  /// 这个是比较万能的函数,但是提供的参数也是非常的多

CGImageRef CGImageCreateWithJPEGDataProvider (

    CGDataProviderRef source,

    const CGFloat decode[],

    bool shouldInterpolate,

    CGColorRenderingIntent intent

);         // 从jpeg源中创建图像

CGImageRef CGImageCreateWithPNGDataProvider (

    CGDataProviderRef source,

    const CGFloat decode[],

    bool shouldInterpolate,

    CGColorRenderingIntent intent

);     // 从png源中创建图像

 

CGImageRef CGImageCreateWithImageInRect (

    CGImageRef image,

    CGRect rect

);     // 从一张图片的某块区域创建图片,类似截图

 

CGImageRef CGImageSourceCreateImageAtIndex (

    CGImageSourceRef isrc,

    size_t index,

    CFDictionaryRef options

);     // 从一个图片源中创建图片,这个图片源可能包含不止一张图片,0表示第一张

 

CGImageRef CGImageSourceCreateThumbnailAtIndex (

    CGImageSourceRef isrc,

    size_t index,

    CFDictionaryRef options

);     // 创建一个缩略图从一个图片源中,这个图片源可能包含不止一张图片,0表示第一张

 

CGImageRef CGBitmapContextCreateImage (

    CGContextRef c

);     // 从一个图形上下文中创建图片

 

CGImageRef CGImageCreateCopy (

    CGImageRef image

);     // 拷贝一张图片

 

CGImageRef CGImageCreateCopyWithColorSpace (

    CGImageRef image,

    CGColorSpaceRef colorspace

);     // 拷贝

一张图片,替换原来的颜色空间

从jpeg创建图片

 

void MyCreateAndDrawBitmapImage (CGContextRef myContext,

                                 CGRect myContextRect,

                                 const char *filename);

{

     CGImageRef image;

     CGDataProviderRef provider;

     CFStringRef path;

     CFURLRef url;

     path = CFStringCreateWithCString (NULL, filename,

                         kCFStringEncodingUTF8);

     url = CFURLCreateWithFileSystemPath (NULL, path, // 2

                             kCFURLPOSIXPathStyle, NO);

     CFRelease(path);

     provider = CGDataProviderCreateWithURL (url);// 3

     CFRelease (url);

     image = CGImageCreateWithJPEGDataProvider (provider,// 4

                                     NULL,

                                     true,

                                    kCGRenderingIntentDefault);

     CGDataProviderRelease (provider);// 5

     CGContextDrawImage (myContext, myContextRect, image);// 6

     CGImageRelease (image);// 7

}

CGImageRef CGImageMaskCreate (

         size_t width,

         size_t height,

         size_t bitsPerComponent,

         size_t bitsPerPixel,

         size_t bytesPerRow,

         CGDataProviderRef provider,

         const float decode[],

         int shouldInterpolate

);  

// 从一个已经存在的图形上下文中创建一个CGLayerRef

 

CGLayerRef CGLayerCreateWithContext (

    CGContextRef context,

    CGSize size,

    CFDictionaryRef auxiliaryInfo

);

CGSize CGLayerGetSize (

    CGLayerRef layer

);

然后从CGLayerRef中得到CGContextRef,就是图形上下文

 

CGContextRef CGLayerGetContext (

    CGLayerRef layer

);

接着在这个CGContextRef中绘画

 

Draw the CGLayer to the Destination Graphics Context

void CGContextDrawLayerInRect (

    CGContextRef context,

    CGRect rect,

    CGLayerRef layer

);

void CGContextDrawLayerAtPoint (

    CGContextRef context,

    CGPoint point,

    CGLayerRef layer

);

Transparency Layers 阴影 
Transparency Layers 给我们很方便的画阴影

void CGContextBeginTransparencyLayer ( CGContextRef context, CFDictionaryRef auxiliaryInfo ); 

函数第二个参数可以传递一些设置参数,没有就传NULL 
看下面的示例代码

CGSize myShadowOffset = CGSizeMake (10, -20);// 设置阴影的偏移量

CGContextSetShadow (myContext, myShadowOffset, 5); // 第三个参数表示透明和模糊度

CGContextBeginTransparencyLayer (myContext, NULL);

CGContextSetRGBFillColor (myContext, 0, 1, 0, 1);

CGContextFillRect (myContext, CGRectMake (wd/3+ 50,ht/2 ,wd/4,ht/4));

CGContextEndTransparencyLayer (myContext);

CGContextBeginTransparencyLayer (myContext, NULL);// 4

CGContextSetRGBFillColor (myContext, 0, 0, 1, 1);

CGContextFillRect (myContext, CGRectMake (wd/3-50,ht/2-100,wd/4,ht/4));

CGContextSetRGBFillColor (myContext, 1, 0, 0, 1);

CGContextFillRect (myContext, CGRectMake (wd/3,ht/2-50,wd/4,ht/4));

CGContextEndTransparencyLayer (myContext);

原文地址:https://www.cnblogs.com/Free-Thinker/p/5112738.html