zoukankan      html  css  js  c++  java
  • iOS基础(十八)——调整图片尺寸和大小

    调整图片尺寸和大小

    /**
     *  调整图片尺寸和大小
     *
     *  @param sourceImage  原始图片
     *  @param maxImageSize 新图片最大尺寸
     *  @param maxSize      新图片最大存储大小
     *
     *  @return 新图片imageData
     */
    + (NSData *)reSizeImageData:(UIImage *)sourceImage maxImageSize:(CGFloat)maxImageSize maxSizeWithKB:(CGFloat) maxSize
    {
        
        if (maxSize <= 0.0) maxSize = 1024.0;
        if (maxImageSize <= 0.0) maxImageSize = 1024.0;
        
        //先调整分辨率
        CGSize newSize = CGSizeMake(sourceImage.size.width, sourceImage.size.height);
        
        CGFloat tempHeight = newSize.height / maxImageSize;
        CGFloat tempWidth = newSize.width / maxImageSize;
        
        if (tempWidth > 1.0 && tempWidth > tempHeight) {
            newSize = CGSizeMake(sourceImage.size.width / tempWidth, sourceImage.size.height / tempWidth);
        }
        else if (tempHeight > 1.0 && tempWidth < tempHeight){
            newSize = CGSizeMake(sourceImage.size.width / tempHeight, sourceImage.size.height / tempHeight);
        }
        
        UIGraphicsBeginImageContext(newSize);
        [sourceImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        //调整大小
        NSData *imageData = UIImageJPEGRepresentation(newImage,1.0);
        CGFloat sizeOriginKB = imageData.length / 1024.0;
        
        CGFloat resizeRate = 0.9;
        while (sizeOriginKB > maxSize && resizeRate > 0.1) {
            imageData = UIImageJPEGRepresentation(newImage,resizeRate);
            sizeOriginKB = imageData.length / 1024.0;
            resizeRate -= 0.1;
        }
        
        return imageData;
    }
    
  • 相关阅读:
    搭建企业级Docker Registry -- Harbor
    搭建私服-docker registry
    CentOS 7.2修改网卡名称
    tomcat错误日志监控脚本
    Openresty+Lua+Redis灰度发布
    Jenkins权限控制-Role Strategy Plugin插件使用
    Rsyslog日志服务搭建
    awk4.0对数组value排序
    Spring-IOC 在非 web 环境下优雅关闭容器
    Spring-IOC bean 生命周期之 Lifecycle 钩子
  • 原文地址:https://www.cnblogs.com/smileK/p/9670527.html
Copyright © 2011-2022 走看看