概述
//1.缩放
- (UIImage *)zoomImage:(UIImage *)image toScale:(CGSize)reSize
{
//根据要显示的大小等比例算出缩放后的图片大小
NSLog(@"image=====w==%fn--------image==%fnn",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==%fn--------h==%fnn",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==%fn--------scaledImage==%fnn",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==%fn--------drawH==%fnn",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
最后
以上就是深情翅膀为你收集整理的对图片的缩放和裁剪的全部内容,希望文章能够帮你解决对图片的缩放和裁剪所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复