需求:很多时候我们需要对拍照后的图片进行处理展示或者上传,但是原图的数据是很大,或者我们要展示的区域很小,这时我们就需要对图片进行简单的处理
做法:1.原图originalImage进行上传
2.缩略图thumImage进行展示
//image是通过系统相机代理方法回调获取拍照的原图
UIImage *originalImage = image;
//计算比例和长宽
CGFloat rate = 200.0/266.0;
CGFloat cw = originalImage.size.width;
CGFloat h = originalImage.size.height;
CGFloat ch = originalImage.size.width*rate;
//originalImagePath-原图的本地存放路径
NSString *originalImagePath = photo.originalImagePath;
//thumImagePath-缩略图的本地存放路径
NSString *thumImagePath = photo.thumImagePath;
//按比例截取中间部分
UIImage *thumImage = [originalImage getSubImage:CGRectMake(0, (h-ch)/2.0, cw, ch)];
//等比例压缩
thumImage = [thumImage scaleToSize:CGSizeMake(266.0, 200.0)];
/*
* UIImageJPEGRepresentation与UIImagePNGRepresentation的区别:
* 1.UIImageJPEGRepresentation处理的图片比UIImagePNGRepresentation小(这是很重要的,上传图片我们考虑的就是体积大小和图片质量,如果上传的图片过大肯定是不好的)
* 2.UIImageJPEGRepresentation有一个缩放系数,可以将图片缩放到能接受的质量,一般在0.3-0.7之间,从而也减小了了图片的大小,而UIImagePNGRepresentation没有这个系数
*/
//写入文件
NSData *imagedata = UIImageJPEGRepresentation(originalImage,0.6);
[imagedata writeToFile:originalImagePath atomically:YES];
NSData *thumImagedata = UIImageJPEGRepresentation(thumImage,1);
[thumImagedata writeToFile:thumImagePath atomically:YES];
上面两个是用于展示剪切后的缩略图的方法:
1.- (UIImage)getSubImage:(CGRect)rect //截取部分图像
2.- (UIImage)scaleToSize:(CGSize)size //等比例缩放
将其抽成UIImage的分类,具体实现:
//处理图片方向(比如横拍,反拍等最后都转成正常方向展示)
- (UIImage *)fixOrientation {
// No-op if the orientation is already correct
if (self.imageOrientation == UIImageOrientationUp)
return self;
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;
switch (self.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, self.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
switch (self.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, self.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
default:
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
CGImageGetBitsPerComponent(self.CGImage), 0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));
CGContextConcatCTM(ctx, transform);
switch (self.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
break;
}
// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
//截取部分图像
- (UIImage*)getSubImage:(CGRect)rect
{
UIImage *image = [self fixOrientation];
CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
UIGraphicsBeginImageContext(smallBounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, smallBounds, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
CGImageRelease(subImageRef);
return smallImage;
}
//等比例缩放
- (UIImage*)scaleToSize:(CGSize)size
{
CGFloat width = CGImageGetWidth(self.CGImage);
CGFloat height = CGImageGetHeight(self.CGImage);
float verticalRadio = size.height*1.0/height;
float horizontalRadio = size.width*1.0/width;
float radio = 1;
if(verticalRadio>1 && horizontalRadio>1)
{
radio = verticalRadio > horizontalRadio ? horizontalRadio : verticalRadio;
}
else
{
radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;
}
width = width*radio;
height = height*radio;
int xPos = (size.width - width)/2;
int yPos = (size.height-height)/2;
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[self drawInRect:CGRectMake(xPos, yPos, width, height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
这两个方法的结合使用应该注意的是rect和size中宽高比要保持一致,这样展示的图片才不会有问题。应该只有宽高比一致,那么- (UIImage*)scaleToSize:(CGSize)size方法中的xPos,yPos就都为0,缩放后的图片才会布满整个size,否则的话就会有留白。
如果我们要展示原图的缩略图(不需要剪切),我们也可以交换两个方法的使用,同样的要保持rect和size中宽高比要保持一致。
本文非原创,来源网络资源与现实项目需求的整合。