【问题标题】:How to make transparent area uiimage in path如何在路径中制作透明区域uiimage
【发布时间】:2019-11-13 04:12:26
【问题描述】:

在 iOS 中,
我有一张图片。
然后,我想在图像的特定路径中制作透明区域。
下图是一些图像。并且,心脏区域是透明区域的随机路径。
我只有一张完整的图片。
我想使图像的特定区域透明。它是 UIImage,而不是 UIImageView。所以,我不能使用遮罩。

https://stackoverflow.com/a/8306254/2160799
我尝试使用此解决方案,但心脏区域被蓝色填充,不透明。 :(
文档是这样解释的。因为解决方案使用 imageContext(不是窗口或位图上下文),所以我不应该使用函数(CGContext.clear(_:))

如果提供的上下文是窗口或位图上下文,Core Graphics 会清除矩形。对于其他上下文类型,Core Graphics 以与设备相关的方式填充矩形。但是,您不应在窗口或位图上下文以外的上下文中使用此函数。

【问题讨论】:

  • UIImage没有imageview不能显示!!
  • @SPatel 我知道。关键是图像处理。
  • 你能解决这个问题吗?

标签: ios swift core-graphics


【解决方案1】:

你可以使用下面的sn-p。在这里,您将使用 api UIGraphicsBeginImageContextWithOptions(_:_:_:) 创建一个图像上下文,并将 opaque 参数指定为 false,这将使最终图像透明。

UIGraphicsBeginImageContextWithOptions(image.size, false, 0)
image.draw(at: .zero)
if let context = UIGraphicsGetCurrentContext() {
    context.addPath(bezierPath.cgPath)
    context.setBlendMode(.clear)
    context.setFillColor(UIColor.clear.cgColor)
    context.fillPath()

    let capturedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}

【讨论】:

  • 我不能。在那种情况下,空白区域被蓝色填充,我描述了。
  • 您是否尝试过使用 UIGraphicsBeginImageContextWithOptions api 创建上下文?
  • 是的,我尝试使用 UIGraphicsBeginImageContextWithOptions API。这是相同的结果。 :(
  • 您确定没有在上下文中绘制任何其他内容吗?
  • func 剪辑(inPath 路径:CGPath)-> UIImage? { UIGraphicsBeginImageContextWithOptions(size, false, 0) defer { UIGraphicsEndImageContext() } draw(at: .zero) guard let context = UIGraphicsGetCurrentContext() else { return nil } context.addPath(path) context.clip() context.clear(CGRect (x: 0, y: 0, width: size.width, height: size.height)) guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return nil } return image }
猜你喜欢
  • 2012-12-20
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 2020-03-07
  • 2015-11-19
  • 2014-10-24
  • 2011-08-31
相关资源
最近更新 更多