【发布时间】:2019-06-09 19:45:19
【问题描述】:
我的应用程序需要从磁盘读取 JPEG 文件,将其裁剪到已知区域,然后使用不同的文件名保存。我当前的实现看起来像这样:
// open and crop the image
let image = CIImage(contentsOf: imagePath)
let cropRect = CGRect(x: 100, y: 100, width: 100, height: 100)
let croppedImage = image.cropped(to: cropRect)
// write to disk
let colorSpace = CGColorSpace(name: CGColorSpace.sRGB)
let context = CIContext()
context.writeJPEGRepresentation(of: croppedImage, to: destination, colorSpace: colorSpace, options: [kCGImageDestinationLossyCompressionQuality as CIImageRepresentationOption: 1])
我发现即使我通过裁剪边缘来减小图像的尺寸,保存的文件也最多是原始文件的两倍。我认为这是因为我正在对其重新采样以另存为新的 JPEG。我还注意到图像略有不同 - 可能不那么清晰。我当然可以调整压缩质量以减小文件大小,但这又会影响图像质量。
有没有什么方法可以裁剪已经压缩的 jpeg 文件而不增加文件大小或更改其任何其他特征?
【问题讨论】:
-
50%?我不这么认为。您说的是 jpeg 而不是未压缩的图像。顺便说一句,50% 的原始图像会产生 1/4 大小的像素
-
好点 - 我的意思是它当然不应该比原来的大!
-
如果生成的图像尺寸是 8 的倍数,可以crop a JPEG without recompression,但需要对原始文件进行操作。 CoreImage 做不到。
标签: ios swift macos uiimage core-image