图像压缩,质量损失最小

时间:2018-08-24 19:40:07

标签: swift xcode image compression

值得澄清的是,我已经用Swift编程语言及其所有组件知道了三天! 我尝试解决此问题的三天时间,我还没有找到一个解决方案,我已经遍及许多站点了!我需要确保在上传到服务器时图像的趣味性不超过150 kb,但同时要保证图像的质量。 在搜索过程中,我在网站和GitHub上发现了一种算法,他们写道,这是WhatsApp Messenger图像压缩的副本,但是在iPhone 8上,它并不能很好地压缩图像。如果举个例子,将iPhone 8上拍摄的图像在传输到Messenger WhatsApp时的压缩率压缩为100 kb。图像质量非常好,长度和宽度都超过1000像素。如果使用此算法,则长度为:800px,宽度为:600px和压缩比为0.01时,图像的重量超过200 kb。而且质量很差。 这是算法:

extension UIImage {
func compressImage() -> UIImage? {
    // Reducing file size to a 10th
    var actualHeight: CGFloat = self.size.height
    var actualWidth: CGFloat = self.size.width
    let maxHeight: CGFloat = 800.0
    let maxWidth: CGFloat = 600.0
    var imgRatio: CGFloat = actualWidth/actualHeight
    let maxRatio: CGFloat = maxWidth/maxHeight
    var compressionQuality: CGFloat = 0.01

    if actualHeight > maxHeight || actualWidth > maxWidth {
        if imgRatio < maxRatio {
            //adjust width according to maxHeight
            imgRatio = maxHeight / actualHeight
            actualWidth = imgRatio * actualWidth
            actualHeight = maxHeight
        } else if imgRatio > maxRatio {
            //adjust height according to maxWidth
            imgRatio = maxWidth / actualWidth
            actualHeight = imgRatio * actualHeight
            actualWidth = maxWidth
        } else {
            actualHeight = maxHeight
            actualWidth = maxWidth
            //compressionQuality = 1
        }
    }
    let rect = CGRect(x: 0.0, y: 0.0, width: actualWidth, height: actualHeight)
    UIGraphicsBeginImageContext(rect.size)
    self.draw(in: rect)
    guard let img = UIGraphicsGetImageFromCurrentImageContext() else {
        return nil
    }
    UIGraphicsEndImageContext()
    guard let imageData = UIImageJPEGRepresentation(img, compressionQuality) else {
        return nil
    }
    return UIImage(data: imageData)
}

}

由于它不适用于该算法,因此我开始寻找Swift的图像压缩库,但不幸的是我什么都没找到!然后,我尝试使用LZFSE压缩算法,但是据我了解,在另一台设备上未使用解压缩器(假设使用Android OS),该图像将不会显示,此外,他抱怨其中一行代码中没有数据。 这是代码:

 public enum CompressionAlgorithm {
  case lz4   // speed is critical
  case lz4a  // space is critical
  case zlib  // reasonable speed and space
  case lzfse // better speed and space
}

private enum CompressionOperation {
  case compression, decompression
}

private func perform(_ operation: CompressionOperation,
                     on input: Data,
                     using algorithm: CompressionAlgorithm,
                     workingBufferSize: Int = 2000) -> Data?  {
  var output = Data()

  // set the algorithm
  let streamAlgorithm: compression_algorithm
  switch algorithm {
    case .lz4:   streamAlgorithm = COMPRESSION_LZ4
    case .lz4a:  streamAlgorithm = COMPRESSION_LZMA
    case .zlib:  streamAlgorithm = COMPRESSION_ZLIB
    case .lzfse: streamAlgorithm = COMPRESSION_LZFSE
  }

  // set the stream operation, and flags
  let streamOperation: compression_stream_operation
  let flags: Int32
  switch operation {
    case .compression:
      streamOperation = COMPRESSION_STREAM_ENCODE
      flags = Int32(COMPRESSION_STREAM_FINALIZE.rawValue)
    case .decompression:
      streamOperation = COMPRESSION_STREAM_DECODE
      flags = 0
  }

  // create a stream
  var streamPointer = UnsafeMutablePointer<compression_stream>
    .allocate(capacity: 1)
  defer {
    streamPointer.deallocate(capacity: 1)
  }

  // initialize the stream
  var stream = streamPointer.pointee
  var status = compression_stream_init(&stream, streamOperation, streamAlgorithm)
  guard status != COMPRESSION_STATUS_ERROR else {
    return nil
  }
  defer {
    compression_stream_destroy(&stream)
  }

  // set up a destination buffer
  let dstSize = workingBufferSize
  let dstPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: dstSize)
  defer {
    dstPointer.deallocate(capacity: dstSize)
  }

  // process the input
  return input.withUnsafeBytes { (srcPointer: UnsafePointer<UInt8>) in

    stream.src_ptr = srcPointer
    stream.src_size = input.count
    stream.dst_ptr = dstPointer
    stream.dst_size = dstSize

    while status == COMPRESSION_STATUS_OK {
      // process the stream
      status = compression_stream_process(&stream, flags)

      // collect bytes from the stream and reset
      switch status {

      case COMPRESSION_STATUS_OK:
        output.append(dstPointer, count: dstSize)
        stream.dst_ptr = dstPointer
        stream.dst_size = dstSize

      case COMPRESSION_STATUS_ERROR:
        return nil

      case COMPRESSION_STATUS_END:
        output.append(dstPointer, count: stream.dst_ptr - dstPointer)

      default:
        fatalError()
      }
    }
    return output
  }
}

// Compressed keeps the compressed data and the algorithm
// together as one unit, so you never forget how the data was
// compressed.  
public struct Compressed {
  public let data: Data
  public let algorithm: CompressionAlgorithm

  public init(data: Data, algorithm: CompressionAlgorithm) {
    self.data = data
    self.algorithm = algorithm
  }

  // Compress the input with the specified algorithm. Returns nil if it fails.
  public static func compress(input: Data,
                              with algorithm: CompressionAlgorithm) -> Compressed? {
    guard let data = perform(.compression, on: input, using: algorithm) else {
      return nil
    }
    return Compressed(data: data, algorithm: algorithm)
  }

  // Factory method to return uncompressed data. Returns nil if it cannot be decompressed.
  public func makeDecompressed() -> Data? {
    return perform(.decompression, on: data, using: algorithm)
  }
}

// For discoverability, add a compressed method to Data
extension Data {
  // Factory method to make compressed data or nil if it fails.
  public func makeCompressed(with algorithm: CompressionAlgorithm) -> Compressed? {
    return Compressed.compress(input: self, with: algorithm)
  }
}

在应用此算法后,在此行中:

UIImage(data: imageCompressData)//к переменной imageCompressData был применен код сверху!

接下来,我不小心偶然发现了以下代码:

func resizeImageUsingVImage(image:UIImage, size:CGSize) -> UIImage? {
    let cgImage = image.cgImage!
    var format = vImage_CGImageFormat(bitsPerComponent: 8, bitsPerPixel: 32, colorSpace: nil, bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.first.rawValue), version: 0, decode: nil, renderingIntent: CGColorRenderingIntent.defaultIntent)
    var sourceBuffer = vImage_Buffer()
    defer {
        free(sourceBuffer.data)
    }
    var error = vImageBuffer_InitWithCGImage(&sourceBuffer, &format, nil, cgImage, numericCast(kvImageNoFlags))
    guard error == kvImageNoError else { return nil }
    // create a destination buffer
    let scale = image.scale
    let destWidth = Int(size.width)
    let destHeight = Int(size.height)
    let bytesPerPixel = image.cgImage!.bitsPerPixel/8
    let destBytesPerRow = destWidth * bytesPerPixel
    let destData = UnsafeMutablePointer<UInt8>.allocate(capacity: destHeight * destBytesPerRow)
    defer {
        destData.deallocate(capacity: destHeight * destBytesPerRow)
    }
    var destBuffer = vImage_Buffer(data: destData, height: vImagePixelCount(destHeight), width: vImagePixelCount(destWidth), rowBytes: destBytesPerRow)
    // scale the image
    error = vImageScale_ARGB8888(&sourceBuffer, &destBuffer, nil, numericCast(kvImageHighQualityResampling))
    guard error == kvImageNoError else { return nil }
    // create a CGImage from vImage_Buffer
    var destCGImage = vImageCreateCGImageFromBuffer(&destBuffer, &format, nil, nil, numericCast(kvImageNoFlags), &error)?.takeRetainedValue()
    guard error == kvImageNoError else { return nil }
    // create a UIImage
    let resizedImage = destCGImage.flatMap { UIImage(cgImage: $0, scale: 0.0, orientation: image.imageOrientation) }
    destCGImage = nil
    return resizedImage
}

再仔细看一下代码,并研究了vImage和Accelerate库的文档,我意识到,如果我有足够的知识,那么这个非常强大的图像处理库也许可以创建一个好的图像压缩器,但是我知识是远远不够的,所以对您的希望全是。

2 个答案:

答案 0 :(得分:0)

在第一个样本中,0.01压缩质量表示质量将是原始图像的1%。

尝试将其值更改为0.8以获得80%的质量,看看效果是否更好。

答案 1 :(得分:0)

由于您真正要做的是确保您上传的质量最高的图像具有最大图像大小(800x600)和数据字节大小(150k),因此将其分为两个单独的步骤:

// size down the image to fit in 800x600
let sizedImage = image.aspectFit(toSize:CGSize(width:800, height:600))

// get the jpeg image with a max size of 150k bytes
let imageData = sizedImage.getJPEGData(withMaxSize:150_000)

当然,如果没有UIImage.aspectFit的定义,那么所有这些实际上都无济于事:

func *(lhs:CGSize, rhs:CGFloat) -> CGSize {
    return CGSize(width: lhs.width * rhs, height: lhs.height * rhs)
}

extension CGSize {
    func aspectFit(toSize:CGSize) -> CGSize {
        return self * min(toSize.width / width, toSize.height / height, 1.0)
    }
}

extension UIImage {
    func aspectFit(toSize:CGSize) -> UIImage? {
        let newSize = size.aspectFit(toSize:toSize)

        UIGraphicsBeginImageContext(newSize)
        defer {
            UIGraphicsEndImageContext()
        }

        draw(in: CGRect(origin: CGPoint.zero, size: newSize))

        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

UIImage.getJPEGData

extension UIImage {
    func getJPEGData(withMaxSize max:Int) -> Data? {
        for quality in stride(from: 1.0 as CGFloat, to: 0.05, by: 0.05) {
            if let data = UIImageJPEGRepresentation(self, quality), data.count < max {
                return data
            }
        }

        return nil
    }
}

这是一种“更智能”的方法,它执行二进制搜索以获得低于150k的最佳图像质量,但是对于大多数用途而言,实际上可能会显得过于矫kill过头:

func binarySearch<Type:Comparable, Result>(lhs:Type, rhs:Type, next:(Type, Type)->Type, predicate:(Type)->Result?) -> (Type, Result)? {
    let middle = next(lhs, rhs)

    if(middle == lhs) {
        return predicate(lhs).map { ( middle, $0) }
    }

    if(predicate(middle) != nil) {
        return binarySearch(lhs: middle, rhs: rhs, next: next, predicate: predicate)
    }
    else {
        return binarySearch(lhs: lhs, rhs: middle, next: next, predicate: predicate)
    }
}

extension UIImage {
    func jpegData(withMaxSize max:Int) -> Data? {
        if let data = UIImageJPEGRepresentation(self, 1.00), data.count < max {
            return data
        }

        guard let (quality, data) = binarySearch(lhs: 1, rhs: 100, next: { ($0 + $1) / 2 }, predicate: { (quality:Int)->Data? in
            guard let data = UIImageJPEGRepresentation(self, CGFloat(quality)/100) else {
                return nil
            }

            return data.count <= max ? data : nil
        }) else {
            return nil
        }

        print("quality = \(quality)")

        return data
    }
}