- #import "UIImageExt.h"
- @implementation UIImage (UIImageExt)
- -(UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
- {
- UIImage*sourceImage = self;
- UIImage*newImage =nil;
- CGSizeimageSize = sourceImage.size;
- CGFloatwidth = imageSize.width;
- CGFloatheight = imageSize.height;
- CGFloattargetWidth = targetSize.width;
- CGFloattargetHeight = targetSize.height;
- CGFloatscaleFactor = 0.0;
- CGFloatscaledWidth = targetWidth;
- CGFloatscaledHeight = targetHeight;
- CGPointthumbnailPoint = CGPointMake(0.0,0.0);
-
- if(CGSizeEqualToSize(imageSize, targetSize) == NO)
- {
- CGFloatwidthFactor = targetWidth / width;
- CGFloatheightFactor = 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
-
- CGRectthumbnailRect = CGRectZero;
- thumbnailRect.origin= thumbnailPoint;
- thumbnailRect.size.width =scaledWidth;
- thumbnailRect.size.height= scaledHeight;
-
- [sourceImagedrawInRect:thumbnailRect];
-
- newImage= UIGraphicsGetImageFromCurrentImageContext();
- if(newImage== nil)
- NSLog(@"couldnot scale image");
-
- //popthe context to get back to the default
- UIGraphicsEndImageContext();
- returnnewImage;
- }
- @end
1、创建UIImage的类别,添加图像压缩指定大小方法imageByScalingAndCroppingForSize:(CGSize)targetSize;此方法传入一个压缩图像大小以后的高度和宽度。(第一部分代码)
2、实现UIImageExt类别,初始化图像缩放默认值。(9-19)
3、然后检查是否需要进行图像缩放。(21)
4、如果需要缩放,则计算先分别计算高度、宽度的因子。(23-24)
5、选择因子系数最大的一边作为图像的缩放因子。(26-29)
6、根据前面得到的图像缩放因子得到缩放图像的等比例高度和宽度值。(30-31)
7、由于是按照最大系数一边进行缩放,造成其中另外一边会出现空白,所以需要计算出另外一边居中显示的坐标值。(34-42)
8、最后根据最新合成的Origin和Size在画布完成内容绘制。(45-52)
9、保存为UIImage对象。(54-56)
10、关闭画图,返回UIImage对象。(59-60)