第一篇博客,总结一下图片处理的一些方法,以供后续补充使用:
一、关于图片压缩:
在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,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小
二、关于图片的重新绘制:
1. 等比缩放:
2. 自定义大小:
只要是继承UIView的object 都可以处理
必须先import QuzrtzCore.framework
4. 根据给定的图片裁剪生成新的图片:
-(UIImage *)getImageFromImage{
//大图bigImage
//定义myImageRect,截图的区域
CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);
UIImage* bigImage= [UIImage imageNamed:@"k00030.jpg"];
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
CGSize size;
size.width = 57.0;
size.height = 57.0;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}
下面补充一些在开发中写得几个方法,实用性稍微强一点的!主要功能实现从图片中心进行剪切!
//剪切图片(从中心算起) - (UIImage *)getCutImageSize:(CGSize)size originalImage:(UIImage *)originalImage{ originalImage = [self equalScaleCompressImage:originalImage size:size]; CGImageRef imageRef = originalImage.CGImage; CGImageRef cutImageRef = CGImageCreateWithImageInRect(imageRef, [self getCutRectWithBigSize:originalImage.size cutRect:size]); UIGraphicsBeginImageContext(size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextDrawImage(context, [self getCutRectWithBigSize:originalImage.size cutRect:size], cutImageRef); UIImage *cutImage = [UIImage imageWithCGImage:cutImageRef]; UIGraphicsEndImageContext(); return cutImage; } //获取截图区域(从中心算起) - (CGRect)getCutRectWithBigSize:(CGSize)bigSize cutRect:(CGSize)cutSize{ CGPoint bigPoint = CGPointMake(bigSize.width / 2.0f, bigSize.height / 2.0f); CGRect Rect = CGRectMake(bigPoint.x - cutSize.width / 2.0f, bigPoint.y - cutSize.height / 2.0f, cutSize.width, cutSize.height); return Rect; } //等比压缩图片 //如果自动适应image的size 以宽、高最大值为主 - (UIImage *)equalScaleCompressImage:(UIImage *)bigImage size:(CGSize)size{ CGFloat scale = [self getCompressScaleWithBigSize:bigImage.size smallSize:size]; UIGraphicsBeginImageContext(CGSizeMake(bigImage.size.width * scale, bigImage.size.height * scale)); [bigImage drawInRect:CGRectMake(0, 0, bigImage.size.width * scale, bigImage.size.height * scale)]; UIImage *scaledImage =UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData *data = UIImageJPEGRepresentation(scaledImage, 1.0); [data writeToFile:@"/Users/tongkun/Desktop/123344" atomically:YES]; return scaledImage; } //获取压缩比scale - (CGFloat)getCompressScaleWithBigSize:(CGSize)bigSize smallSize:(CGSize)smallSize{ CGFloat scale; if (bigSize.height / bigSize.width >= smallSize.height / smallSize.width) { scale = smallSize.width / bigSize.width; }else{ scale = smallSize.height / bigSize.height; } return scale; }