【问题标题】:Drawing into a scaled CGContext results in all-white image绘制成缩放的 CGContext 会导致全白图像
【发布时间】:2018-06-16 10:52:39
【问题描述】:

如果我将图像绘制到 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()

【问题讨论】:

    标签: ios cocoa-touch core-graphics


    【解决方案1】:

    您已经在 Y 轴上镜像了图像。所以点 (1, 0) 现在是 (-1, 0)。这意味着一切都(几乎可以肯定)画在边界之外,在 X 平面的负半部分。

    如果您希望“右侧边缘”为零,您还需要将上下文转换为屏幕宽度。比如:

    context.translateBy(x: bounds.width, y:0)
    

    (未经测试)

    【讨论】:

    • 哦,呵呵。当然。我的脑海中有UIView.transform 的心理模型。谢谢!
    猜你喜欢
    • 2019-09-17
    • 2012-10-06
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2021-05-15
    • 1970-01-01
    • 2012-06-06
    相关资源
    最近更新 更多