第一种 
先用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);

CGContextRef CIImageRef详解的更多相关文章

  1. iOS中-Qutarz2D详解及使用

    在iOS中Qutarz2D 详解及使用 (一)初识 介绍 Quartz 2D是二维绘图引擎. 能完成的工作有: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成 ...

  2. iOS 2D绘图详解(Quartz 2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)

    前言:一个路径可以包含由一个或者多个shape以及子路径subpath,quartz提供了很多方便的shape可以直接调用.例如:point,line,Arc(圆弧),Curves(曲线),Ellip ...

  3. 详解iOS开发之自定义View

    iOS开发之自定义View是本文要将介绍的内容,iOS SDK中的View是UIView,我们可以很方便的自定义一个View.创建一个 Window-based Application程序,在其中添加 ...

  4. OpenGL ES一些函数详解(一)

    glLoadIdentity和glMultMatrix   glLoadIdentity的作用是将当前模型视图矩阵转换为单位矩阵(行数和列数相同的矩阵,并且矩阵的左上角至右下角的连线上的元素都为1,其 ...

  5. iOS学习——(转)UIResponder详解

    本文转载自:ios开发 之 UIResponder详解 我们知道UIResponder是所有视图View的基类,在iOS中UIResponder类是专门用来响应用户的操作处理各种事件的,包括触摸事件( ...

  6. ios学习--详解IPhone动画效果类型及实现方法

    详解IPhone动画效果类型及实现方法是本文要介绍的内容,主要介绍了iphone中动画的实现方法,不多说,我们一起来看内容. 实现iphone漂亮的动画效果主要有两种方法,一种是UIView层面的,一 ...

  7. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  8. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  9. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

随机推荐

  1. git的0基础使用

    1.申请一个git帐号 2.项目开发者将你增加这个项目 3.在终端随意一个目录克隆 该项目地址 git clone 该项目地址 4.进nginx配置 5.更新的时候进入项目目录 git pull

  2. 『转载』Debussy快速上手(Verdi相似)

    『转载』Debussy快速上手(Verdi相似) Debussy 是NOVAS Software, Inc(思源科技)发展的HDL Debug & Analysis tool,这套软体主要不是 ...

  3. Android入门3:从Toolbar到Material Design

    在Android5.0(API 21)之后,Toolbar被Google推广,逐渐走入大家视野.具体关于Actionbar和Toolbar的对比就不多啰嗦了,跟着潮流走是没错的.下面先上张简单的效果图 ...

  4. HTML5画布(圆形)

    案例1: <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8& ...

  5. DataTable 导出Excel 下载 (NPOI)

        public class ExcelHelper { public void DownLoadExcelNew(System.Data.DataTable data, Hashtable h, ...

  6. wordpress安装地址与博客地址

    可千万别乱改动你的wordpress安装地址和博客地址 一月 27th, 2009 Posted in web学习, 博客建设 | 12 Comments » 我最近一个月学习数据库的一些使用,把他爱 ...

  7. Trucking(HDU 2962 最短路+二分搜索)

    Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. Hdu1076(n个闰年后的年份)

    #include <stdio.h> #include<stdlib.h> int main() { int T,Y,n,printYear; scanf("%d&q ...

  9. js+jquery+html实现在三种不通的情况下,点击图片放大的效果

    js+jquery+html实现在三种不通的情况下,点击图片放大的效果. 三种情况分别是:图片的父元素宽高固定;  图片的宽高固定;  图片的父元素宽固定,高度不固定 第一种情况:图片的父元素宽高固定 ...

  10. Android中图片的目录

    图片放在hdpi及在mdpi中的效果是不一样的 http://blog.csdn.net/loongggdroid/article/details/9469935