ios图片缩放,根据UIImageView Frame的尺寸,自动调整UIImage缩放,保证图片显示不变形

我们再开发的过程中经常会碰到显示图片的情况,但是如果我们从网络上取到图片后不进行任何处理直接把得到的UIImage赋值给UIImageView显示的时候就会出现变形的情况,如果服务器端已经把图片的宽高处理好了,直接返回给前端显示那可以不考虑这种情况。但如果服务器返回的图片列表尺寸不一致的话,本地是需要处理的,具体的实现,参照以下步骤,如果有更好的实现和不足的地方,望各位指正。以下代码是我在具体的项目开发中已经应用了的。

1.建立一个UIImage的Category,UIImageScale。

2.定义一个外部访问方法,.h文件如下。

#import <UIKit/UIKit.h>


@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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值