ios - 绘制到缩放的 CGContext 中会产生全白图像

标签 ios cocoa-touch core-graphics

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

相关文章:

在 Swift iOS 中解码 PHP JSON 返回空值

ios - UICollectionView header view like table view header

ios - 真实设备上的 Core Graphics 游戏性能

ios - 在 viewController 上使用自定义类 (drawrect)

ios - EXC_BAD_ACCESS 在停止 MPMoviePlayerController 时崩溃

ios - 当我尝试对最多 100 张图像执行屏幕外旋转时,CIIImage 泄漏

iphone - shouldStartLoadWithRequest 附加链接与 applewebdata

ios - 详细的电池使用情况-iOS

ios - 在我的应用程序中向左/向右滑动会触发将数据保存到我的数据库的操作。然而,快速滑动会导致许多保存失败

ios - CGContextSetFillColorWithColor : invalid context 0x0