//1.缩放
- (UIImage *)zoomImage:(UIImage *)image toScale:(CGSize)reSize
{
//根据要显示的大小等比例算出缩放后的图片大小
NSLog(@"image=====w==%f\n--------image==%f\n\n",image.size.width,image.size.height);
ZoomInEqualPercent *ziep = [[ZoomInEqualPercent alloc] init];
CGSize size_max = CGSizeMake(reSize.width, reSize.height);
CGSize size_new = [ziep zoomSizeWithMin:image.size :size_max];
NSLog(@"size_new=====w==%f\n--------h==%f\n\n",size_new.width,size_new.height);
//绘制这个大小的图片
UIGraphicsBeginImageContext(size_new);
[image drawInRect:CGRectMake(0,0, size_new.width, size_new.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"scaledImage=====w==%f\n--------scaledImage==%f\n\n",scaledImage.size.width,scaledImage.size.height);
return scaledImage;
}
- (UIImage *)scaleImage:(UIImage *)image toScale:(CGSize)reSize
{<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px 'Heiti SC Light'; color: #008212"><span style="font: 16.0px Menlo"]]>//</span>先按要显示的大小<span style="font: 16.0px Menlo"> </span>去比例缩放下图片</p>
UIImage *scaledImage = [self zoomImage:image toScale:reSize];
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px 'Heiti SC Light'; color: #008212"><span style="font: 16.0px Menlo"]]>//</span>计算截取位置。这里我们考虑一般拍照边界位置可能存在全白或全黑的情况,多数重要的会在中间位置。所以计算下截取是重绘图片的中间位置</p>
float drawW = 0.0;
float drawH = 0.0;
CGSize size_new = scaledImage.size;
if (size_new.width > reSize.width) {
drawW = (size_new.width - reSize.width)/2.0;
}
if (size_new.height > reSize.height) {
drawH = (size_new.height - reSize.height)/2.0;
}
NSLog(@"drawW=====w==%f\n--------drawH==%f\n\n",drawW,drawH);
//截取截取大小为需要显示的大小。取图片中间位置截取
CGRect myImageRect = CGRectMake(drawW, drawH, reSize.width, reSize.height);
UIImage* bigImage= scaledImage;
scaledImage = nil;
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
UIGraphicsBeginImageContext(reSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}
转载于:https://www.cnblogs.com/xubojoy/p/3930191.html