gpt4 book ai didi

ios - UIImageJPEGRepresentation 增加 UIImage 大小

转载 作者:行者123 更新时间:2023-11-30 12:32:26 26 4
gpt4 key购买 nike

我有一个图像(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com