http://mp.weixin.qq.com/s/A900w0Y5pGjuaB4j9Os9ww1、UIImage 生成方法的对比

Apple官方的文档为生成一个UIImage对象提供了两种方法:

1. imageNamed,其参数为图片的名字;优点:加载时会缓存图片,用于图片的频繁使用;缺点:占用内存,直到应用结束

2. imageWithContentsOfFile,其参数也是图片文件的路径。优点:仅加载图片,不会缓存,用于价值次数较少的情况,降低内存消耗

那么两种有什么区别吗?

肯定是有的。根据Apple的官方文档:

imageNamed: 这个方法用一个指定的名字在系统缓存中查找并返回一个图片对象如果它存在的话。如果缓存中没有找到相应的图片,这个方法从指定的文档中加载然后缓存并返回这个对象。因此imageNamed的优点是当加载时会缓存图片。所以当图片会频繁的使用时,那么用imageNamed方法会比较好。例如:你需要在 一个TableView里的TableViewCell里都加载同样一个图标,那么用imageNamed加载图像效率很高。系统会把那个图标Cache到内存,在TableViewCell里每次利用那个图 像的时候,只会把图片指针指向同一块内存。正是因此使用imageNamed会缓存图片,即将图片的数据放在内存中,iOS的内存非常珍贵并且在内存消耗过大时,会强制释放内存,即会遇到memory warnings。而在iOS系统里面释放图像的内存是一件比较麻烦的事情,有可能会造成内存泄漏。例如:当一 个UIView对象的animationImages是一个装有UIImage对象动态数组NSMutableArray,并进行逐帧动画。当使用imageNamed的方式加载图像到一个动态数组NSMutableArray,这将会很有可能造成内存泄露。原因很显然的。

imageWithContentsOfFile:仅加载图片,图像数据不会缓存。因此对于较大的图片以及使用情况较少时,那就可以用该方法,降低内存消耗。

下面列举出两种方法的详细用法:

  1. NSString *path = [[NSBundle mainBundle] pathForResource:@”icon” ofType:@”png”];
  2. UIImage *image = [UIImage imageWithContentsOfFile:path];

以及:

  1. NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:“png”];
  2. NSData *image = [NSData dataWithContentsOfFile:filePath];
  3. UIImage *image = [UIImage imageWithData:image]; //or = [UIImage imageWithContentsOfFile:filePath];
IOS中对图片的处理  UIImage
相信做项目时肯定会有用到 UIImage 这个类,那我们就来看一下这个类中都有什么内容。
其实这篇文章就是在看文档的时候想记录一下文档中得方法。
UIImage  继承于NSObject
下面介绍一下UIImage中的方法
首先是我们最常用的
 
通过图片的文件名来获取这个图片
+ (UIImage *)imageNamed:(NSString *)name
//要注意的是这个方法适用于已经导入到工程中的图片
 
创建新图片
 
 
 
1、+ (UIImage *)imageWithContentsOfFile:(NSString *)path
//通过文件加载指定路径下的文件内容获得新图片
 
2、+ (UIImage *)imageWithData:(NSData *)data
//通过一个NSData对象来获得图片
3、+ (UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale
//同上,只是再加上一个图片大小比例,用来改变图片的大小
 
4、+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage
//使用Quartz 2D对象创建UIImage
5、+ (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
//制定图片的比例和方向,其中方向是个枚举类。
 
6、+ (UIImage *)imageWithCIImage:(CIImage *)ciImage
//用一个Core Image 对象创建图像
7、+ (UIImage *)imageWithCIImage:(CIImage *)ciImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
//再加上比例和图片方向
 
8、- (UIImage *)imageWithAlignmentRectInsets:(UIEdgeInsets)alignmentInsets
//返回指定矩形区域内的图像
 
9、+ (UIImage *)animatedImageNamed:(NSString *)name duration:(NSTimeInterval)duration
//创建一个动态图片,动态图片持续的时间为duration
10、+ (UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration
//用一组图片创建一个动态图片,动态持续时间duration
 
11、+ (UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets duration:(NSTimeInterval)duration
//创建一个在可变大小的图片上指定矩形区域内的动态图片
12、+ (UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode duration:(NSTimeInterval)duration
//同上,只是多了一个图片变化的方式,具体来说就是平铺或者拉伸
 
13、- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
//用制定矩形区域创建图像
14、- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
//同上,指定图片变化方式
 
初始化图片 
 
 
 
方法的作用在从上面的一些方法中都能找到原型,这里就不一一注释了
1、– initWithContentsOfFile:  //从文件加载图片
2、– initWithData:                  //用NSData对象初始化图片
3、– initWithData:scale:      //用NSData对象,指定的比例,初始化图片
4、– initWithCGImage:
5、– initWithCGImage:scale:orientation:
6、– initWithCIImage:
7、– initWithCIImage:scale:orientation:
 
 
绘画图片
 
1、– drawAtPoint:
//在指定的点开始绘画图片,这个点就是图片的做上角顶点
2、- (void)drawAtPoint:(CGPoint)point blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha
//在指定的点绘制整个图片,并使用自定义图片复合模式,并设置透明度
 
3、– drawInRect:
//在指定区域内绘制图片,可根据需要缩放图片
4、– drawInRect:blendMode:alpha:
//参照上面第二条
5、– drawAsPatternInRect:
//在指定区域内,平铺图片
 
 
image的属性
  imageOrientation   //图片的方向
   size   //图片的大小size
   scale   //图片的比例
   resizingMode  //图片变化方式
   CGImage  //潜在的Quartz image
   CIImage  //潜在的Core Image
   images  //返回一个由图片组成的数组,针对于由一组图片生成的动态图片
   duration  //返回动态图片持续的时间(即动态图片播放一遍的时间)
   capInsets  //图片上选定的区域
   alignmentRectInsets  //图片平铺的区域
 
iOS自带的提供了一个API如下
[html] view plaincopy
NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);   
在Iphone上有两种读取图片数据的简单方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.而UIImagePNGRepresentation只需要图片引用作为参数.通过在实际使用过程中,比较发现: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的图片数据量大很多.譬如,同样是读取摄像头拍摄的同样景色的照片, UIImagePNGRepresentation()返回的数据量大小为199K ,而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的数据量大小只为140KB,比前者少了50多KB.如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentation函数的第二个参数,大幅度降低图片数据量.譬如,刚才拍摄的图片, 通过调用UIImageJPEGRepresentation(UIImage* image, 1.0)读取数据时,返回的数据大小为140KB,但更改压缩系数后,通过调用UIImageJPEGRepresentation(UIImage* image, 0.5)读取数据时,返回的数据大小只有11KB多,大大压缩了图片的数据量 ,而且从视角角度看,图片的质量并没有明显的降低.因此,在读取图片数据内容时,建议优先使用UIImageJPEGRepresentation,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小。
 
[html] view plaincopy
UIImage *imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
imageNew = [self imageWithImage:imageNew scaledToSize:CGSizeMake(100, 100)]; 
NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.0001); 
   
m_selectImage = [UIImage imageWithData:imageData]; 
 
 
.h具体code
  
[html] view plaincopy
#import <Foundation/Foundation.h> 
   
@interface UIImage (UIImageExt) 
   
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size; 
   
- (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize; 
@end 
.m具体code
[html] view plaincopy
#import "UIImageExt.h" 
   
   
@implementation UIImage (UIImageExt) 
   
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{ 
    // 创建一个bitmap的context 
    // 并把它设置成为当前正在使用的context 
    UIGraphicsBeginImageContext(size); 
    // 绘制改变大小的图片 
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    // 从当前context中创建一个改变大小后的图片 
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    // 使当前的context出堆栈 
    UIGraphicsEndImageContext(); 
    // 返回新的改变大小后的图片 
    return scaledImage; 
   
   
   
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize 
    UIImage *sourceImage = self
    UIImage *newImage = nil
    CGSize imageSize = sourceImage.size; 
    CGFloat width = imageSize.width; 
    CGFloat height = imageSize.height; 
    CGFloat targetWidth = targetSize.width; 
    CGFloat targetHeight = targetSize.height; 
    CGFloat scaleFactor = 0.0; 
    CGFloat scaledWidth = targetWidth; 
    CGFloat scaledHeight = targetHeight; 
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 
       
    if (CGSizeEqualToSize(imageSize, targetSize) == NO
    
        CGFloat widthFactor = targetWidth / width; 
        CGFloat heightFactor = targetHeight / height; 
           
        if (widthFactor > heightFactor) 
            scaleFactor = widthFactor; // scale to fit height 
        else 
            scaleFactor = heightFactor; // scale to fit width 
        scaledWidth  = width * scaleFactor; 
        scaledHeight = height * scaleFactor; 
           
        // center the image 
        if (widthFactor > heightFactor) 
        
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
        
        else 
            if (widthFactor < heightFactor) 
            
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
            
    
       
    UIGraphicsBeginImageContext(targetSize); // this will crop 
       
    CGRect thumbnailRect = CGRectZero; 
    thumbnailRect.origin = thumbnailPoint; 
    thumbnailRect.size.width  = scaledWidth; 
    thumbnailRect.size.height = scaledHeight; 
       
    [sourceImage drawInRect:thumbnailRect]; 
       
    newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    if(newImage == nil
        NSLog(@"could not scale image"); 
       
    //pop the context to get back to the default 
    UIGraphicsEndImageContext(); 
    return newImage; 
  <br>/** 返回一张经过处理的图片*/<br>+ (UIImage *)createCompressesImageWithName:(NSString *)imageName<br>{<br>    <br>    UIImage *newImage = [UIImage imageWithImageSimple:[UIImage imageWithName:imageName] scaledToSize:[UIImage imageWithName:imageName].size];<br><br>    return newImage;<br>}<br>
@end

UIImage类方法总结及UIImage生成方法对比的更多相关文章

  1. 国内app快速生成平台对比

     泰格老虎 2013-03-07 00:39:10 这是海恒CEO高鹏写的一篇国内app快速生成平台对比文章,介绍了国内快速生成APP的平台与自己平台的对比,很有参考价值. 同类网站 安米网 http ...

  2. 多个UIImage合并成一个UIImage

    多个UIImage合并成一个UIImage 创建两个UIImage UIImage *image1 = [UIImage imageNamed:@"iOSDevTip"]; UII ...

  3. GDP区域分布图的生成与对比(ArcPy实现)

    一.背景 各地区经济协调发展是保证国民经济健康持续稳定增长的关键.GDP是反映各地区经济发展状况的重要指标.科学准确分析各地区GDP空间分布特征,对制定有效措施,指导经济协调发展具有重要参考价值. 二 ...

  4. SQL Server开发接口生成方法

    为提高开发效率,生成固定格式的接口是必须的,以下以提供新增/修改/删除/读取接口为例: 以常见的表结构为例,特殊表结构可自己尝试去调整方法 主要通过系视图 sys.columns生成方法:为包含列的对 ...

  5. 关于全局唯一ID生成方法

    引:最近业务开发过程中需要涉及到全局唯一ID生成.之前零零总总的收集过一些相关资料,特此整理以便后用 本博客已经迁移至:http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...

  6. 细聊分布式ID生成方法

    细聊分布式ID生成方法 https://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=403837240&idx=1&sn=ae9 ...

  7. 分布式环境下的id生成方法

    分布式环境下的id生成方法   前几天研究数据库分表分库的问题,其中有一个关键的地方就是生成唯一键的问题,假如数据表有1亿条数据,而且还在不断的增加,这里我们就需要考虑到分表分库,假设我们采用Hash ...

  8. Linux和Windows下查看环境变量方法对比

    摘自:Linux和Windows下查看环境变量方法对比 一.查看所有环境变量的名称和值 Linux下:export Windows下:set 二.根据名称查该环境变量的值 Linux下:echo $环 ...

  9. C语言清空输入缓冲区的N种方法对比

    转自C语言清空输入缓冲区的N种方法对比 C语言中有几个基本输入函数: //获取字符系列 int fgetc(FILE *stream); int getc(FILE *stream); int get ...

随机推荐

  1. 《Metasploit魔鬼训练营》第四章(下)

    p163 XSSF 默认kali 2.0中没有xssf,先下载:https://code.google.com/archive/p/xssf/downloads 将下载下来的zip文件解压,将其中的d ...

  2. LINQ学习系列-----2.1 一个Linq语句

    Linq语句介绍 先上源码: 上述代码涵盖了Linq新特性: 代码解析: 针对本文中的几点特性,前面有文章进行阐述.

  3. 彻底理解线程同步与同步代码块synchronized

    public class Demo { public static synchronized void fun1(){ } public synchronized void fun2(){ } pub ...

  4. 11个优秀的Android开发开源项目

    一. 一个类似微信的时光轴效果   时光轴效果 项目地址 https://github.com/ljtyzhr/TimeLine 二. 安卓选择器类库,包括日期.时间.单项.双项选择器.城市地址选择器 ...

  5. HDU 1698 Just a Hook 线段树+lazy-target 区间刷新

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37

    一:开始Nginx的SSL模块 1.1 Nginx如果未开启SSL模块,配置Https时提示错误 1 nginx: [emerg] the "ssl" parameter requ ...

  7. mongo安装,及远程连接

    yum 安装mongo创建镜像 1 vi /etc/yum.repos.d/mongodb-org-3.6.repo添加一下路径 保存退出[mongodb-org-3.6]name=MongoDB R ...

  8. React学习笔记--程序调试

    React学习笔记 二 程序调试   前面我们搭建好了React的基本开发环境,可以编写基本的React js程序了.但完成的开发环境肯定包含调试器,怎么调试用React编写的JS程序呢?有浏览器,比 ...

  9. mac下出现xcrun: error导致git、svn无法使用的解决办法

    现象:xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun ...

  10. 查看SQL Server数据读写分离,并设置读写分离

    1. 查看读写分离脚本,直接执行以下脚本: select name,is_read_committed_snapshot_on from sys.databases 执行结果列表中,name表示数据库 ...