如何清除右下角的细边线?

时间:2019-02-04 13:18:29

标签: ios core-graphics mask cgpath

我希望将椭圆形减去矩形后的复合视图形状,

所以首先我创建一个遮罩图像:

    let paths = CGMutablePath()
    //Oval shape path at left
    let leftPath = CGPath(ellipseIn: CGRect(x: 0, y: 0, width: 200, height: 150), transform: nil)

    //Rect shape path at right
    let rightPath = CGPath(roundedRect: CGRect(x: 100, y: 100, width: 120, height: 100), cornerWidth: 8, cornerHeight: 8, transform: nil)

    paths.addPath(leftPath)
    paths.addPath(rightPath)

    UIGraphicsBeginImageContext(CGSize(width: 220, height: 200))
    let ctx = UIGraphicsGetCurrentContext()

    ctx?.addPath(paths)
    ctx?.clip(using: .evenOdd)

    ctx?.addPath(leftPath.cgPath)
    ctx?.clip(using: .evenOdd)

    ctx?.setFillColor(UIColor.red.cgColor)
    ctx?.fill(CGRect(x: 0, y: 0, width: 220, height: 200))

    //the mask image
    let maskImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

接下来,我将此图像用作遮罩:

    let maskView = UIImageView(image: maskImage)
    maskView.contentMode = .center    
    imageView.mask = maskView

运行App,我明白了:

enter image description here

看起来不错?不是真的...

如果仔细看,您会在右下角看到一条细线

这对我来说不好!

如何删除边线???谢谢:)

1 个答案:

答案 0 :(得分:0)

绘制椭圆,然后清除圆角的矩形。

import UIKit

let ellipse = CGPath(ellipseIn: CGRect(x: 0, y: 0, width: 200, height: 150), transform: nil)
let roundedRect = CGPath(roundedRect: CGRect(x: 100, y: 100, width: 120, height: 100), cornerWidth: 8, cornerHeight: 8, transform: nil)

let maskImage = UIGraphicsImageRenderer(size: CGSize(width: 220, height: 200)).image { rendererContext in
    let ctx = rendererContext.cgContext

    ctx.setFillColor(UIColor.red.cgColor)
    ctx.addPath(ellipse)
    ctx.fillPath()

    ctx.setBlendMode(.clear)
    ctx.addPath(roundedRect)
    ctx.fillPath()
}

结果:

playground output