ios--图片压缩/缩放

object-c图片压缩

头文件 ImageUtil.h

@interface ImageUtil
+(UIImage *)imageWithCompressImage:(UIImage *)image
+(UIImage *)imageWithScaleImage:(UIImage *)image forSize:(CGSize)targetSize
@end

 方法的实现 ImageUtil.m

#import "ImageUtil.h"
#define MAX_IMAGEPIX 500
@implementation ImageUtil
+(UIImage *)imageWithCompressImage:(UIImage *)image
{
   CGSize imageSize = image.size;
   CGFloat width = imageSize.width;
   CGFloat height = imageSize.height;
   
   if(width <= MAX_IMAGEPIX && height <= MAX_IMAGEPIX){
       return image;//不需要压缩
   }
   
   if(width == 0 || height == 0){
       return image;
   }

   UIImage *newImage = nil;
   CGFloat widthFactor = MAX_IMAGEPIX / width;
   CGFloat heightFactor = MAX_IMAGEPIX / height;
   CGFloat compressFactor = 0.0;

   if(widthFactor > heightFactor){
       compressFactor = heightFactor;//compress to fit height
   }else{
       compressFactor = widthFactor;//compress to fit width
   }

   CGFloat compressedWidth = width * compressFactor;
   CGFloat compressedHeight = height * compressFactor;
   CGSize targetSize = CGSizeMake(compressedWidth,compressedHeight);

   UIGraphicsBeginImageContext(targetSize);//this will crop

   CGRect thumbnailRect = CGRectZero;
   thumbnailRect.size.width = compressWidth;
   thumbnailRect.size.height = compressHeight;
  
   [image drawInRect:thumbnailRect];

   newImage = UIGraphicsGetImageFromCurrentImageContext();
   
   UIGraphicsEndImageContext();//pop the context to get back to the default

   if(newImage != nil){
     return newImage;
   }else{
     return image;
   }
}

+(UIImage *)imageWithScaleImage:(UIImage *)image forSize:(CGSize)targetSize
{
   CGSize imageSize = image.size;

   if(CGSizeEqualToSize(imageSize,targetSize) == YES){
      return image;
   }

   CGFloat width = imageSize.width;
   CGFloat height = imageSize.height;
   CGFloat scaleWidth = targetSize.width;
   CGFloat scaleHeight = targetSize.height;
   
   if(width <= scaleWidth && height <= scaleHeight){
       return image;
   }
   
   if(width == 0 || height == 0){
       return image;
   }

   UIImage *newImage = nil;
   CGFloat widthFactor = scaleWidth / width;
   CGFloat heightFactor = scaleHeight / height;
   CGFloat scaleFactor = 0.0;

   if(widthFactor > heightFactor){
       scaleFactor = heightFactor;//scale to fit height
   }else{
       scaleFactor = widthFactor;//scale to fit width
   }

   CGFloat scaledWidth = width * scaleFactor;
   CGFloat scaledHeight = height * scaleFactor;
   CGSize target = CGSizeMake(scaledWidth,scaledHeight);

   UIGraphicsBeginImageContext(target);//this will crop

   CGRect thumbnailRect = CGRectZero;
   thumbnailRect.size.width = compressWidth;
   thumbnailRect.size.height = compressHeight;
  
   [image drawInRect:thumbnailRect];

   newImage = UIGraphicsGetImageFromCurrentImageContext();
   
   UIGraphicsEndImageContext();//pop the context to get back to the default

   if(newImage != nil){
     return newImage;
   }else{
     return image;
   }
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值