zoukankan      html  css  js  c++  java
  • 最新swift4.0 图片进行尺寸大小及体积压缩

    ///图片压缩方法

       class func resetImgSize(sourceImage : UIImage,maxImageLenght : CGFloat,maxSizeKB : CGFloat) -> Data {

        var maxSize = maxSizeKB

        var maxImageSize = maxImageLenght

        

        if (maxSize <= 0.0) {

            maxSize = 1024.0;

        }

        if (maxImageSize <= 0.0)  {

            maxImageSize = 1024.0;

        }

        //先调整分辨率

        var newSize = CGSize.init( sourceImage.size.width, height: sourceImage.size.height)

        let tempHeight = newSize.height / maxImageSize;

        let tempWidth = newSize.width / maxImageSize;

        if (tempWidth > 1.0 && tempWidth > tempHeight) {

            newSize = CGSize.init( sourceImage.size.width / tempWidth, height: sourceImage.size.height / tempWidth)

        }

        else if (tempHeight > 1.0 && tempWidth < tempHeight){

            newSize = CGSize.init( sourceImage.size.width / tempHeight, height: sourceImage.size.height / tempHeight)

        }

        UIGraphicsBeginImageContext(newSize)

        sourceImage.draw(in: CGRect.init(x: 0, y: 0, newSize.width, height: newSize.height))

        let newImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        var imageData = UIImageJPEGRepresentation(newImage!, 1.0)

        var sizeOriginKB : CGFloat = CGFloat((imageData?.count)!) / 1024.0;

        //调整大小

        var resizeRate = 0.9;

        while (sizeOriginKB > maxSize && resizeRate > 0.1) {

            imageData = UIImageJPEGRepresentation(newImage!,CGFloat(resizeRate));

            sizeOriginKB = CGFloat((imageData?.count)!) / 1024.0;

            resizeRate -= 0.1;

        }

        return imageData!

       }

  • 相关阅读:
    spring MVC 整合mongodb
    Spring Data MongoDB example with Spring MVC 3.2
    SpringMVC整合Mongodb开发 架构搭建
    spring MVC、mybatis配置读写分离
    淘宝网架构分享总结[转]
    淘宝分布式数据层:TDDL[转]
    基于XMPP实现的Openfire的配置安装+Android客户端的实现[转]
    基于开源 Openfire 聊天服务器
    bootstrapUI
    京东手机webapp商城
  • 原文地址:https://www.cnblogs.com/qizhuo/p/8242589.html
Copyright © 2011-2022 走看看