我的应用程序需要从磁盘读取一个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文件?