ios - 如何根据顶部正面图像裁剪图像?

标签 ios swift uiview uiimage

我的目的是从 UIView 中导出 UIImage。所以我想在红色圆圈上添加清晰的边框,使输出图像看起来像第二张图像。

enter image description here 现在我想要这样的东西:我想在红色圆圈上添加清晰的背景,使输出图像看起来像下图。 (我知道如果我添加清晰的边框那么我无法实现这一点所以请建议我实现这一点)

enter image description here

这里我只加了颜色,这2种颜色其实是图片

编辑

我可以使用下面的代码来做到这一点,但切割区域不是四舍五入的。

func cut(hole: CGRect, inView v: UIView) {
    let p:CGMutablePath = CGMutablePath()
    p.addRect(CGRect.init(x: v.frame.origin.x, y: v.frame.origin.y, width: v.frame.width, height: v.frame.height))
    p.addRect(hole)

    let s = CAShapeLayer()
    s.path = p
    s.fillRule = CAShapeLayerFillRule.evenOdd

    v.layer.mask = s
}

这是我的演示链接: https://gofile.io/?c=jukO6B

当前输出

enter image description here

如有任何帮助,我们将不胜感激。

最佳答案

最后我根据要求对这个答案做了一些修改得到了解决方案:How can I 'cut' a transparent hole in a UIImage?

func cut(hole: CGRect, inView v: UIView) {
    let p:CGMutablePath = CGMutablePath()
    v.clipsToBounds = false
    p.addRect(CGRect.init(x: v.frame.origin.x, y: v.frame.origin.y, width: v.frame.width, height: v.frame.height))
    p.addRoundedRect(in: CGRect.init(x: self.vwCarContainer.frame.origin.x, y: self.vwCarContainer.frame.origin.y, width: self.vwCarContainer.frame.width, height: self.vwCarContainer.frame.height), cornerWidth: self.vwCarContainer.layer.cornerRadius, cornerHeight: self.vwCarContainer.layer.cornerRadius)

    let s = CAShapeLayer()
    s.path = p
    s.fillRule = CAShapeLayerFillRule.evenOdd

    v.layer.mask = s
}

感谢所有帮助我实现这一目标的人。

关于ios - 如何根据顶部正面图像裁剪图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58374618/

相关文章:

ios - 尝试设置 UILabel 的文本时,结果始终为 'Optional("0. 0")'

ios - 如何转换 Tableview 单元格中的删除选项

swift - 继承父工厂方法

iOS/Xcode——drawRect : Not Being Called in UIView Subclass

iphone - 我可以重用 UIView 吗?

iphone - 如何在IB中使用UIScrollView?

android - React Native - 如何本地化应用名称?

ios - 设置图像属性后,UIImageView 图像不会明显更新

ios - 如何显示来自 graph api 的 user_friends 数据?

ios - 如何计算特定 iPhone 上的像素到点的转换?