5种创建UIImage的类方法

来源:互联网 发布:如何注册两个淘宝账号 编辑:程序博客网 时间:2024/05/22 04:43

 1. 使用类方法imageNamed:创建

+(UIImage *)imageNamed:(NSString *)name;

name是照片名称。创建UIImage对象 ,代码如下:

UIImage *image = [UIImage imageNamed:@"Jihong"];

使用imageNamed:初始化的时候,会先检查缓存中是否存在Jihong的照片,如果不存在,图片首先会被缓存起来,然后才返回要加载的图片对象;如果存在,直接返回要加载的照片对象。



2.使用类方法imageWithContentsOfFile:创建

+(UIImage*)imageWithContentOfFile:创建

path是需要加载照片的路径,如何获取照片的路径应该获取沙盒路径。实现代码如下:

UIImage *image = [UIImage imageWithContentOfFile:path];

使用imageWithContentOfFile:创建UIImage的时候,是直接从磁盘上加载。当收到内存警告时,UIImage对象会被释放,下一次绘图的时候,需要重新加载。



3.使用类方法imageWithData:创建

+(UIImage*)imageWithData:(NSData *)data;

data是照片数据,一般是请求返回,然后通过imageWithData:创建UIImage。实现代码如下所示:

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.baidu.com/images.jpg"] ];

UIImage *image = [UIImage imageWithData:data];



4.使用类方法imageWithCGImage:创建

+(UIImage*)imageWithCGImage:(CGImageRef)cgImage;

cgImage 是定义在QuartzCore框架中的一个结构体指针。这个结构用来创建像素位图,可以通过操作存储的像素位来编辑图片

UIImage *image = [UIImage imageWithCGImage:cgImage];



5 使用类方法imageWithCIImage:创建

+(UIImage*)imageWithCIImage:(CIImage*)ciimage

CIImage 是CoreImage框架中最基本代表图像的对象,在CIImage被CIContext渲染出来之前,他是依赖于滤镜链的,滤镜是不会更改CIImage中的图像数据。

UIImage *image = [UIImage imageWithCGImage:ciImage];

以上四种方法都是UIImage的类方法,使用UIImage的初始化方法也是可以创建UIImage对象的。












0 0
原创粉丝点击