gpt4 book ai didi

ios - 在不重新采样的情况下裁剪 JPEG 图像

转载 作者:行者123 更新时间:2023-11-28 13:36:43 26 4
gpt4 key购买 nike

我的应用程序需要从磁盘读取 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 文件而不增加文件大小或改变其任何其他特征?

最佳答案

您的方法看起来是正确的。由于 JPEG 压缩的性质,您不能“只裁剪”图像而不在此过程中对其进行解码和重新编码。

较大的文件大小应该是由您选择的压缩质量造成的。引用文档:

A value of 1.0 specifies to use lossless compression if destination format supports it […]

1.0 质量的 JPEG 图像通常比 PNG 图像大。根据我的经验,介于 0.7 和 0.85 之间的值会产生良好的质量与尺寸平衡。

关于ios - 在不重新采样的情况下裁剪 JPEG 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56517873/

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