服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - ios中图像进行压缩方法汇总

ios中图像进行压缩方法汇总

2020-12-19 16:17iOS教程网 IOS

在Iphone上有两种读取图片数据的简单方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.而UIImagePNGRepresentation只需要图片引用作为参数.

方法一:

 

复制代码 代码如下:

- (UIImage*)scaleFromImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
 CGSize imageSize = image.size;
 CGFloat width = imageSize.width;
 CGFloat height = imageSize.height;
     
 if (width <= newSize.width && height <= newSize.height){
  return image;
 }
     
 if (width == 0 || height == 0){
  return image;
 }
     
 CGFloat widthFactor = newSize.width / width;
 CGFloat heightFactor = newSize.height / height;
 CGFloat scaleFactor = (widthFactor<heightFactor?widthFactor:heightFactor);
     
 CGFloat scaledWidth = width * scaleFactor;
 CGFloat scaledHeight = height * scaleFactor;
 CGSize targetSize = CGSizeMake(scaledWidth,scaledHeight);
     
    UIGraphicsBeginImageContext(targetSize);
    [image drawInRect:CGRectMake(0,0,scaledWidth,scaledHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

 

方法二:

.h具体code

复制代码 代码如下:

#import <Foundation/Foundation.h> 
@interface UIImage (UIImageExt) 
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size; 
- (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize; 
@end 

 

.m具体code

 

复制代码 代码如下:

#import "UIImageExt.h" 
@implementation UIImage (UIImageExt) 
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{ 
    // 创建一个bitmap的context 
    // 并把它设置成为当前正在使用的context 
    UIGraphicsBeginImageContext(size); 
    // 绘制改变大小的图片 
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    // 从当前context中创建一个改变大小后的图片 
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    // 使当前的context出堆栈 
    UIGraphicsEndImageContext(); 
    // 返回新的改变大小后的图片 
    return scaledImage; 

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize 

    UIImage *sourceImage = self; 
    UIImage *newImage = nil; 
    CGSize imageSize = sourceImage.size; 
    CGFloat width = imageSize.width; 
    CGFloat height = imageSize.height; 
    CGFloat targetWidth = targetSize.width; 
    CGFloat targetHeight = targetSize.height; 
    CGFloat scaleFactor = 0.0; 
    CGFloat scaledWidth = targetWidth; 
    CGFloat scaledHeight = targetHeight; 
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 
    if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
    { 
        CGFloat widthFactor = targetWidth / width; 
        CGFloat heightFactor = targetHeight / height; 
        if (widthFactor > heightFactor) 
            scaleFactor = widthFactor; // scale to fit height 
        else 
            scaleFactor = heightFactor; // scale to fit width 
        scaledWidth  = width * scaleFactor; 
        scaledHeight = height * scaleFactor; 
        // center the image 
        if (widthFactor > heightFactor) 
        { 
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
        } 
        else 
            if (widthFactor < heightFactor) 
            { 
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
            } 
    } 
    UIGraphicsBeginImageContext(targetSize); // this will crop 
    CGRect thumbnailRect = CGRectZero; 
    thumbnailRect.origin = thumbnailPoint; 
    thumbnailRect.size.width  = scaledWidth; 
    thumbnailRect.size.height = scaledHeight; 
    [sourceImage drawInRect:thumbnailRect]; 
    newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    if(newImage == nil) 
        NSLog(@"could not scale image"); 
    //pop the context to get back to the default 
    UIGraphicsEndImageContext(); 
    return newImage; 

@end 

 

 

方法三:(本人项目中使用的方法)

 

复制代码 代码如下:

-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth
{
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = defineWidth;
    CGFloat targetHeight = (targetWidth / width) * height;
    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    [sourceImage drawInRect:CGRectMake(0,0,targetWidth,  targetHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

 

 

以上所述就是本文的全部内容了,希望大家能够喜欢。

延伸 · 阅读

精彩推荐
  • IOS谈一谈iOS单例模式

    谈一谈iOS单例模式

    这篇文章主要和大家谈一谈iOS中的单例模式,单例模式是一种常用的软件设计模式,想要深入了解iOS单例模式的朋友可以参考一下...

    彭盛凇11872021-01-19
  • IOSiOS10 Xcode8适配7个常见问题汇总

    iOS10 Xcode8适配7个常见问题汇总

    这篇文章主要为大家详细汇总了iOS10 Xcode8适配7个常见问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    索马里猫10332021-02-01
  • IOSIOS网络请求之AFNetWorking 3.x 使用详情

    IOS网络请求之AFNetWorking 3.x 使用详情

    本篇文章主要介绍了IOS网络请求之AFNetWorking 3.x 使用详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    总李写代码6892021-03-04
  • IOSiOS APP实现微信H5支付示例总结

    iOS APP实现微信H5支付示例总结

    这篇文章主要介绍了iOS APP实现微信H5支付示例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    一张小A11332021-06-01
  • IOSiOS逆向教程之logify跟踪方法的调用

    iOS逆向教程之logify跟踪方法的调用

    这篇文章主要给大家介绍了关于iOS逆向教程之logify跟踪方法调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    Mr.Guo11472021-04-28
  • IOSxcode8提交ipa失败无法构建版本问题的解决方案

    xcode8提交ipa失败无法构建版本问题的解决方案

    xcode升级到xcode8后发现构建不了新的版本。怎么解决呢?下面小编给大家带来了xcode8提交ipa失败无法构建版本问题的解决方案,非常不错,一起看看吧...

    Cinna丶7542021-02-03
  • IOSiOS中时间与时间戳的相互转化实例代码

    iOS中时间与时间戳的相互转化实例代码

    这篇文章主要介绍了iOS中时间与时间戳的相互转化实例代码,非常具有实用价值,需要的朋友可以参考下。...

    张无忌!4812021-03-09
  • IOSiOS常见的几个修饰词深入讲解

    iOS常见的几个修饰词深入讲解

    这篇文章主要给大家介绍了关于iOS常见的几个修饰词的相关资料,iOS修饰词包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通过示...

    郡王丶千夜7422021-05-10