UIImage与CGImage的相关知识点总结

本文详细介绍了UIImage支持的各种图片格式,包括JPEG、GIF、PNG等,并提供了常用的类方法说明,如从bundle加载图片、从网络加载图片等。此外还探讨了UIImage与CGImageRef之间的关系及其转换方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                          第一部分 UIImage支持的图片格式


      格式                                         后缀

  JPEG格式                                      .jpg  .jpeg

  GIF格式                                        .gif

  PNG格式                                       .png

  windos位图格式                             .bmp  .BMPf

  windows图标格式                           .ico

  windows光标格式                           .cur

  标签图像文件格式                            .tiff .tif 


UIIMage常用来显示图片的类方法如下:

一  从 main bundle 加载指定文件名对应的图片,但是这个方法有缓存,当试图去加载的文件不存在的时候,他才会去加载图片文件并缓存它,如果已经缓存,则直接使用已缓存好的图片

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

二  这个方法根据指定的路径中去加载文件对应的图片

+ (UIImage *)imageWithContentsOfFile:(NSString *)path;


三  根据NSData中的图片数据来加载对应的图片

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


四 该方法根据NSData中的图片数据来加载对应的图片,并且会按照指定的缩放因子对图片进行缩放

+ (UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale NS_AVAILABLE_IOS(6_0);


五  根据指定的CGImageRef对象来创建UIImage

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

 

六  根据指定的CGImageRef对象来创建UIImage,并且会按照指定的缩放因子对图片进行缩放和旋转

+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation NS_AVAILABLE_IOS(4_0);

  

                         第二部分 加载图片的方式



一 从bundle中加载对应的图片


UIImage* image=[UIImage imageNamed:@"1.png"];


二 从网络加载图片


NSURL* url    =[NSURL URLWithString:@"1131907198420455d6o.jpg"];

NSData* data  =[NSData dataWithContentsOfURL:url];

UIImage* image=[UIImage imageNamed:data];


三 从手机上加载图片


NSString* path=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComopnoent:@"baidu.png"];

UIImage* image=[UIIMage imageWithContentsOfFile: path];


四 加载多张图片实现动画的效果


该方法根据指定的图片名来加载系列图片

// animated images. When set as UIImageView.image, animation will play in an infinite loop until removed. Drawing will render the first image


+ (UIImage *)animatedImageNamed:(NSString *)name duration:(NSTimeInterval)duration NS_AVAILABLE_IOS(5_0);  // read sequence of files with suffix starting at 0 or 1


该方法要求传入一个数组,作为多张动画图片,数组中的每个元素都是UIImage对象

+ (UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration NS_AVAILABLE_IOS(5_0);


                        第三部分 UIIMage与CGImage的关系


UIImage的缺点:不能对图片进行缩放,旋转,不能挖取原图片的指定区域。需要借助CGImageRef来实现、


CGImageRef不是面向对象的API,而是一个指针类型

131123_RGHE_2332019.png


UIImage与CGImageRef相互转换


一 将image装换成CGImageRef


UIImage* image=.......

CGImageRef imageRef=[image CGImage];


二 将CGImageref转换成UIImage


CGIMageRef ref=....

UIImage* image=[UIImage imageWithCGImage:ref];



转载于:https://my.oschina.net/iOSliuhui/blog/517040

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值