IOS中对图片的处理 UIImage
相信做项目时肯定会有用到 UIImage 这个类,那我们就来看一下这个类中都有什么内容。
其实这篇文章就是在看文档的时候想记录一下文档中得方法。
UIImage 继承于
NSObject
下面介绍一下UIImage中的方法
首先是我们最常用的
通过图片的文件名来获取这个图片
+ (UIImage *)imageNamed:(
NSString
*)name
创建新图片
1、+ (UIImage *)imageWithContentsOfFile:(
NSString
*)path
2、+ (UIImage *)imageWithData:(
NSData
*)data
3、+ (UIImage *)imageWithData:(
NSData
*)data scale:(CGFloat)scale
4、+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage
5、+ (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
6、+ (UIImage *)imageWithCIImage:(CIImage *)ciImage
7、+ (UIImage *)imageWithCIImage:(CIImage *)ciImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
8、- (UIImage *)imageWithAlignmentRectInsets:(UIEdgeInsets)alignmentInsets
9、+ (UIImage *)animatedImageNamed:(
NSString
*)name duration:(
NSTimeInterval
)duration
10、+ (UIImage *)animatedImageWithImages:(
NSArray
*)images duration:(
NSTimeInterval
)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:
3、– initWithData:scale:
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
scale
resizingMode
CGImage
CIImage
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{
UIGraphicsBeginImageContext(size);
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
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;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
if
(widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
if
(widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize);
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"
);
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