我们再开发的过程中经常会碰到显示图片的情况,但是如果我们从网络上取到图片后不进行任何处理直接把得到的UIImage赋值给UIImageView显示的时候就会出现变形的情况,如果服务器端已经把图片的宽高处理好了,直接返回给前端显示那可以不考虑这种情况。但如果服务器返回的图片列表尺寸不一致的话,本地是需要处理的,具体的实现,参照以下步骤,如果有更好的实现和不足的地方,望各位指正。以下代码是我在具体的项目开发中已经应用了的。
1.建立一个UIImage的Category,UIImageScale。
2.定义一个外部访问方法,.h文件如下。
#import
@interface UIImage (UIImageScale)
-(UIImage*)getSubImage:(CGRect)rect;
-(UIImage*)scaleToSize:(CGSize)size;
-(UIImage *)getImgByViewFrame:(CGRect)viewFrame;
@end
3.以下是.m文件的具体实现。
//
// UIImage+UIImageScale.m
// wdsjd
//
// Created by apple on 15/11/19.
// Copyright © 2015年 Harrison. All rights reserved.
//
#import "UIImage+UIImageScale.h"
@implementation UIImage (UIImageScale)
//-----------------------------------------------------------------------------------------------------------------------------
//根据传入的UIImageView的frame计算图片的一个缩放比例,进行截取,返回截取后的图片。
//-----------------------------------------------------------------------------------------------------------------------------
-(UIImage *)getImgByViewFrame:(CGRect)viewFrame
{
float width = self.size.width;
float height = self.size.height;
float vWidth = CGRectGetWidth(viewFrame);
float vHeight = CGRectGetHeight(viewFrame);
float subW = 0;
float subH = 0;
float subX = 0;
float subY = 0;
if (vWidth == vHeight)
{
if (width < height)
{
subW = width;
subH = width;
}else
{
subW = height;
subH = height;
}
if (width > subW)
{
subX = (width - subW ) /2;
}
if (height > subH)
{
subY =(height - subH) /2;
}
}else
{
float beforeW = vWidth;
float beforeH = vHeight;
float wtimes = width / vWidth;
float htimes = height / vHeight;
float startTimes = wtimes;
if (wtimes > htimes)
{
startTimes = htimes;
}
float tempW = (vWidth * startTimes);
float tempH = (vHeight * startTimes);
beforeW = tempW;
beforeH = tempH;
if (width > beforeW)
{
subX = (width - beforeW ) /2;
}
if (height > beforeH)
{
subY =(height - beforeH) /2;
}
subW = beforeW;
subH = beforeH;
}
if (subW <1)
{
subW = 1;
}
if (subH <1)
{
subH = 1;
}
if (subX < 0)
{
subX = 0;
}
if (subY < 0)
{
subY = 0;
}
@try
{
// CGRect rect = AVMakeRectWithAspectRatioInsideRect(self.size, CGRectMake(0, 0, CGRectGetWidth(viewFrame), MAXFLOAT));
UIImage *image = [self getSubImage:CGRectMake(subX, subY, subW, subH)];
// UIImage *image = [self getSubImage:rect];
return image;
}@catch (NSException * e) {
NSLog(@"--exceptioin->%@",e.reason);
return nil;
}
}
//------------------------------------------------------------------------------------------------------------------
//截取部分图像,根据传入的CGRect 进行截图图片对应的部分。
//------------------------------------------------------------------------------------------------------------------
-(UIImage*)getSubImage:(CGRect)rect
{
CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
if (subImageRef)
{
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;
}else
{
return nil;
}
}
@end