码迷,mamicode.com
首页 > 其他好文 > 详细

UIImage类方法总结及UIImage生成方法对比

时间:2017-02-12 17:11:19      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:图片播放   文件名   1.0   利用   style   getwidth   objc   make   cgpoint   

1、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];  

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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.0   利用   style   getwidth   objc   make   cgpoint   

原文地址:http://www.cnblogs.com/xujiahui/p/6391217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!