美文网首页
swift 二分法压缩图片 2022-07-21

swift 二分法压缩图片 2022-07-21

作者: 漫步的老狼 | 来源:发表于2022-07-21 16:29 被阅读0次
        /// - maxLength: 指定data最大的体积
        func compressImageMid(maxLength: Int,image:UIImage?) -> Data? {
            var compression: CGFloat = 1
                 let minCompression: CGFloat = 0.1
                 var imageData = image?.jpegData(compressionQuality: compression)
                 if imageData?.count ?? 0 < maxLength {
                     return imageData
                 }
                 while imageData?.count ?? 0 > maxLength, compression > minCompression {
                     compression -= 0.1
                     imageData = image?.jpegData(compressionQuality: compression)
                 }
                 if imageData?.count ?? 0 > maxLength {
                     return nil
                 }
                 return imageData
            }
    

    相关文章

      网友评论

          本文标题:swift 二分法压缩图片 2022-07-21

          本文链接:https://www.haomeiwen.com/subject/mcouirtx.html