iOS 图片压缩

ios 的开发中,会经常遇到处理图片的问题,所以我提供了一个可以根据比例、大小进行压缩图片的方法,代码如下

A,//传入的参数:1、生成图片的大小 2、压缩比 3、存放图片的路径

+ (void)createThumbImage:(UIImage *)image size:(CGSize )thumbSize percent:(float)percent toPath:(NSString *)thumbPath{

        CGSize imageSize = image.size;

     CGFloat width = imageSize.width;

     CGFloat height = imageSize.height;

        CGFloat scaleFactor = 0.0;

     CGPoint thumbPoint = CGPointMake(0.0,0.0);

     CGFloat widthFactor = thumbSize.width / width; 

     CGFloat heightFactor = thumbSize.height / height; 

     if (widthFactor > heightFactor)  {

       scaleFactor = widthFactor;

     }

     else {

       scaleFactor = heightFactor;

     }

     CGFloat scaledWidth  = width * scaleFactor; 

     CGFloat scaledHeight = height * scaleFactor;

     if (widthFactor > heightFactor) 

    

       thumbPoint.y = (thumbSize.height - scaledHeight) * 0.5;  

    

     else if (widthFactor < heightFactor) 

    

       thumbPoint.x = (thumbSize.width - scaledWidth) * 0.5; 

    

      UIGraphicsBeginImageContext(thumbSize); 

     CGRect thumbRect = CGRectZero; 

     thumbRect.origin = thumbPoint;

     thumbRect.size.width  = scaledWidth; 

     thumbRect.size.height = scaledHeight;

     [image drawInRect:thumbRect];

    

     UIImage *thumbImage = UIGraphicsGetImageFromCurrentImageContext();

     UIGraphicsEndImageContext();

    NSData *thumbImageData = UIImageJPEGRepresentation(thumbImage, percent);

     [thumbImageData writeToFile:thumbPath atomically:NO];

}

B,下面的这个方法适用于对压缩后的图片的质量要求不高或者没有要求,因为这种方法只是压缩了图片的大小
+(UIImage *)scale:(UIImage *)image toSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
C,

//压缩图片

+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize

{

    // Create a graphics image context

    UIGraphicsBeginImageContext(newSize);

    // Tell the old image to draw in this new context, with the desired

    // new size

    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    // Get the new image from the context

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    // End the context

    UIGraphicsEndImageContext();

    // Return the new image.

    return newImage;

}

 

cell.typeImageView.imageURL = [NSURL URLWithString:@"http://qq.5068.com/uploads/allimg/130107/111Qa446-13.jpg"];

    NSData  *data = UIImagePNGRepresentation(cell.typeImageView.image);

    NSLog(@"--==== data ==  %d", data.length);

    cell.typeImageView.image = [UnityClass imageWithImageSimple:cell.typeImageView.image scaledToSize:CGSizeMake(80, 80)];

    NSData  *data1 = UIImagePNGRepresentation(cell.typeImageView.image);

    NSLog(@"--==== data1 ==  %d", data1.length);

通过打印NSData压缩前后的长度来看是否压缩.


这段“许靖昕”先生分享的代码将示范如何缩小 UIImage

  1. @implementation UIImage (Extras)  
  2. #pragma mark  
  3. #pragma mark Scale and crop image  
  4. (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize  
  5.  
  6. UIImage *sourceImage self;  
  7. UIImage *newImage nil;          
  8. CGSize imageSize sourceImage.size;  
  9. CGFloat width imageSize.width;  
  10. CGFloat height imageSize.height;  
  11. CGFloat targetWidth targetSize.width;  
  12. CGFloat targetHeight targetSize.height;  
  13. CGFloat scaleFactor 0.0;  
  14. CGFloat scaledWidth targetWidth;  
  15. CGFloat scaledHeight targetHeight;  
  16. CGPoint thumbnailPoint CGPointMake(0.0,0.0);  
  17. if (CGSizeEqualToSize(imageSize, targetSize) == NO)   
  18.          
  19.         CGFloat widthFactor targetWidth width;  
  20.         CGFloat heightFactor targetHeight height;  
  21.         if (widthFactor heightFactor)   
  22.                 scaleFactor widthFactor; // scale to fit height  
  23.         else  
  24.                 scaleFactor heightFactor; // scale to fit width  
  25.         scaledWidth  width scaleFactor;  
  26.         scaledHeight height scaleFactor;  
  27.         // center the image  
  28.         if (widthFactor heightFactor)  
  29.                  
  30.                 thumbnailPoint.y (targetHeight scaledHeight) 0.5;   
  31.                  
  32.         else   
  33.                 if (widthFactor heightFactor)  
  34.                          
  35.                         thumbnailPoint.x (targetWidth scaledWidth) 0.5;  
  36.                          
  37.                 
  38. UIGraphicsBeginImageContext(targetSize); // this will crop  
  39. CGRect thumbnailRect CGRectZero;  
  40. thumbnailRect.origin thumbnailPoint;  
  41. thumbnailRect.size.width  scaledWidth;  
  42. thumbnailRect.size.height scaledHeight;  
  43. [sourceImage drawInRect:thumbnailRect];  
  44. newImage UIGraphicsGetImageFromCurrentImageContext();  
  45. if(newImage == nil)   
  46.         NSLog(@"could not scale image");  
  47. //pop the context to get back to the default  
  48. UIGraphicsEndImageContext();  
  49. return newImage;  
  50.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值