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!

       }

  • 相关阅读:
    phalcon之视图缓存
    Java NIO框架Netty教程(一) – Hello Netty
    setsockopt的作用
    支持向量机通俗导论(理解SVM的三层境地)
    quartz中的corn表达式(转)
    Applet 数字签名技术全然攻略
    SJTU 3001. 二哥的幸运
    OGRE之跳出漫长的编译等待
    VB.NET 数组的定义 动态使用 多维数组
    【Python】用Python的“结巴”模块进行分词
  • 原文地址:https://www.cnblogs.com/qizhuo/p/8242589.html
Copyright © 2011-2022 走看看