ios - 如何擦除右下角的细边线?
问题描述
我想要从椭圆形减去矩形形状的复合视图的形状,
所以首先我创建一个蒙版图像:
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
运行应用程序,我得到这个:
看起来不错?不是真的...
如果你仔细看,你会发现右下角有一条细线
对我来说不行!
怎么抹掉边线???谢谢 :)
解决方案
绘制椭圆,然后清除圆角矩形。
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()
}
结果:
推荐阅读
- python - Matplotlib,散布着三个图例
- swift - Swift - 以编程方式刷新约束
- java - 如何配置 EC2 以在我的 Spring Boot 应用程序中处理表情符号
- python - 如何在 tkinter 窗口中绘制我的 networkx 图?
- r - like 函数因调用而异 R
- c++ - gcc 7.4.0 的奇怪 std::atof 错误
- php - Laravel - 如何在我的 Laravel 代码中从 json 中提取值
- python - 期望用双引号括起来的属性名称
- android - Flutter:构建 APK 后,来自 API 的数据不会出现
- php - Wordpress:从自定义帖子类型中删除帖子时更改消息