我有一个图像(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)
}发布于 2017-04-11 07:39:17
是的,这个代码是用于图像压缩的。
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)")
}https://stackoverflow.com/questions/43338978
复制相似问题