UIImageJPEGRepresentation增加了UIImage的大小

时间:2017-04-11 07:06:38

标签: ios swift uiimage uiimagejpegrepresentation

我有一个图像(captureImage),由相机拍摄并调整大小。我使用UIImageJPEGRepresentation压缩图像,压缩后,图像的大小增加。这是正常的行为还是我以错误的方式进行压缩/调整大小?

if let image = captureImage {
    print(image.size) // (700, 700)
    let compressedImageData = UIImageJPEGRepresentation(image, 0.3)!
    let compressedImage = UIImage(data: compressedImageData)!
    print(compressedImage.size) // (3024, 3024)
}

1 个答案:

答案 0 :(得分:0)

是的,此代码用于图像压缩

  let compressedImageData = UIImageJPEGRepresentation(image, 0.3)!

获取文件大小的一种方法是保存到文档目录中。 例如 -

  let filePath = fileURL
        var fileSize : UInt64

        do {
            //return [FileAttributeKey : Any]
            let attr = try FileManager.default.attributesOfItem(atPath: filePath.path)
            fileSize = attr[FileAttributeKey.size] as! UInt64

            //if you convert to NSDictionary, you can get file size old way as well.
            let dict = attr as NSDictionary
            fileSize = dict.fileSize()
            print(fileSize)

        } catch {
            print("Error: \(error)")
        }