我正在使用以下命令裁剪图像:
UIGraphicsBeginImageContext(croppingRect.size)
let context = UIGraphicsGetCurrentContext()
context?.clip(to: CGRect(x: 0, y: 0, width: rect.width, height: rect.height))
image.draw(at: CGPoint(x: -rect.origin.x, y: -rect.origin.y))
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()裁剪的图像有时在右边框和下边框上有一个1px的白色边缘。右下角放大以查看各个像素,如下所示。显然,边框不是纯白色的,而是可能来自后来压缩的白色阴影。

这个白边的伪影是从哪里来的?
发布于 2017-07-27 21:06:47
问题是croppingRect的值不是全像素。
作为x、y、width、height的值,在计算CGFloat数字时,结果有时会是小数(例如1.3而不是1.0)。解决方案是对这些值进行舍入:
let cropRect = CGRect(
x: x.rounded(.towardZero),
y: y.rounded(.towardZero),
width: width.rounded(.towardZero),
height: height.rounded(.towardZero))舍入必须是.towardZero (参见here的意思),因为裁剪矩形不应该(通常)大于图像矩形。
https://stackoverflow.com/questions/45349143
复制相似问题