Ios Swift:将图像写入文件夹后,即使压缩图像,图像大小也会增加
我正在将从照片中拾取的图像保存到文档文件夹中Ios Swift:将图像写入文件夹后,即使压缩图像,图像大小也会增加,ios,swift,uiimagepickercontroller,nsfilemanager,Ios,Swift,Uiimagepickercontroller,Nsfilemanager,我正在将从照片中拾取的图像保存到文档文件夹中 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { guard let selectedImage = info[.originalImage] as? UIImage else { fatalEr
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
selectedImage.jpegData(compressionQuality: 0.0).fileSize() //Printing size as 4284217 bytes|4.3 MB
if let imageData = selectedImage.jpeg(.lowest) {
imageData.fileSize() //Printing size as 4284217 bytes|4.3 MB
let compressedImage = UIImage(data: imageData)?.fixOrientation()
saveImageInDocuments(compressedImage!)
}
picker.dismiss(animated: true, completion: nil)
}
选择图像并压缩到最小大小后,我打印了图像的大小。下一步是将图像保存到Documents文件夹
func saveImageInDocuments(_ image: UIImage) {
let imageName = String(Date().toMillis())
let fileURL = documentsDirectoryURL.appendingPathComponent("\(CUST_APP_DOC)/\(imageName).png")
print(fileURL.path)
if !FileManager.default.fileExists(atPath: fileURL.path) {
do {
try image.pngData()!.write(to: fileURL)
print("Saved filename: \(imageName).png size: \(calculateFileSize(path: String(describing: fileURL)).0)")
} catch {
print(error)
}
} else {
print("Image Not Added")
}
}
用于获取图像大小并计算路径中保存的图像大小的方法
func calculateFileSize(path: String) -> (String, Double) {
let absolutePath = path.replacingOccurrences(of: "file://", with: "")
let fileAttributes = try! FileManager.default.attributesOfItem(atPath: absolutePath)
let fileSizeNumber = fileAttributes[FileAttributeKey.size] as! NSNumber
let fileSize = fileSizeNumber.int64Value
var sizeMB = Double(fileSize / 1024)
sizeMB = Double(sizeMB / 1024)
print(String(format: "%.2f", sizeMB) + " MB")
return (String(format: "%.2f", sizeMB) + " MB", sizeMB)
}
extension Data {
func fileSize() {
let bcf = ByteCountFormatter()
bcf.allowedUnits = [.useMB] // optional: restricts the units to MB only
bcf.countStyle = .file
let string = bcf.string(fromByteCount: Int64(self.count))
print("Printing size as \(self.count) bytes|\(string) MB")
}
}
问题是,在选择图像后,图像文件大小为4.3 MB,但在将图像保存到文件夹中之后。图像大小即将到来“保存的文件名:1546593850872.png大小:29.86 MB”
为什么会这样?如果有人能帮我解释这一点,我会感谢这个人。在搜索了一整天之后,我找到了这个场景的答案 在
didFinishPickingMediaWithInfo
中,我将图像压缩为JPEG格式,图像大小正在减小。然后,在将图像写入目录saveImageInDocuments
时,我使用这行代码再次将JPEG图像转换为PNG
试试image.pngData()!。写入(到:fileURL)
将图像转换为PNG会增加其大小
对于解决方案,我修改了此方法:
func saveImageInDocuments(_ imageData: Data) {
let imageName = String(Date().toMillis())
let fileURL = documentsDirectoryURL.appendingPathComponent("\(CUST_APP_DOC)/\(imageName).png")
print(fileURL.path)
if !FileManager.default.fileExists(atPath: fileURL.path) {
do {
try imageData.write(to: fileURL)
} catch {
print(error)
}
} else {
print("Image Not Added")
}
}
直接保存压缩的JPEG图像。您必须压缩图像大小,然后将其存储在文档目录中,这就是您要执行的操作?在检查您正在调用的压缩数据大小后,此处缺少信息。
.fixOrientation()
,这是做什么的?什么是CreateUploadingPdf()
?@Scriptable.fixOrientation()
用于更改图像的方向。在我的例子中,图像方向在某些情况下发生了变化CreateUploadingPdf()
我已经删除了它,尽管这是一个定义了calculateFileSize()
的类。@AbhishekJadhav是的,我正在做的就是将图像写入目录。图像大小正在增加。