如果我将图像绘制到 CGContext 中,当我稍后用它制作图像时,它会按预期工作。但是当我想通过将 X 轴缩放 -1 来镜像此图像时,生成图像的相同代码会生成全白图像 - 即。不起作用。我错过了什么?
let context = CGContext(data: nil, width: Int(aspectFitSize.width), height: Int(aspectFitSize.height), bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
context.scaleBy(x: -1, y: 1) // <-- this line causes the problem; it's fine without it, but I want to mirror the image...
context.draw(cgImage, in: CGRect.init(origin: .zero, size: aspectFitSize))
let image = context.makeImage()
最佳答案
您已经在 Y 轴上镜像了图像。所以点 (1, 0) 现在是 (-1, 0)。这意味着一切(几乎可以肯定)都在边界之外,在 X 平面的负半部分。
如果您希望“右手边缘”为零,则还需要将上下文平移到屏幕的宽度。像这样的东西:
context.translateBy(x: bounds.width, y:0)
(未经测试)
关于ios - 绘制到缩放的 CGContext 中会产生全白图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48133716/