ios - UIImageJPEGRepresentation 增加 UIImage 大小

标签 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)
}

最佳答案

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

  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)")
        }

关于ios - UIImageJPEGRepresentation 增加 UIImage 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43338978/

相关文章:

swift - 具有适用于 iOS 和 MacOS 目标的共享 CoreData 的 Xcode 项目

ios - 导入 UIKit 并使用图像作为 AppEnum 的 caseDisplayRepresentations 时出现 AppIntents 问题

swift - 如果使用默认值初始化类成员来完成相同的工作,那么方便初始化程序有什么好处

ios - NSCFArray 长度错误 iOS

ios - 我怎样才能重绘 UIView 的形状并同时给它一个阴影?

ios -\n 对于 NSString 不适用于 NSData 中的字符串

ios - 如何向部分和分页部分添加两个新单元格?

ios - 一段文本中每个单词的音频

ios - 从 NSArray 更改 UIImageView 中的 UIImage

ios - 快速将基于 RGB 值的 N 图像中的特定像素更改为不同的 RBG 颜色