【发布时间】:2018-08-05 00:56:51
【问题描述】:
我是 Swift 的初学者,我想使用 UIGraphicsImageRenderer 将图像添加到自定义路径绘制中。我给你看我的代码:
let size = CGSize(width:newWidth , height:height)
let f = UIGraphicsImageRendererFormat.default()
f.opaque = false
let r = UIGraphicsImageRenderer(size:size, format: f)
let im = r.image { _ in
let leftUpPath = UIBezierPath()
leftUpPath.move(to: CGPoint(x: 0, y: 0))
leftUpPath.addArc(withCenter: CGPoint(x: cornerRadiusView, y: cornerRadiusView),
radius: cornerRadiusView,
startAngle: .pi * 3 / 2,
endAngle: .pi,
clockwise: false)
leftUpPath.addLine(to: CGPoint(x: 0, y: height))
leftUpPath.addLine(to: CGPoint(x: newWidth - cutPicture, y: height))
leftUpPath.addLine(to: CGPoint(x: newWidth, y: 0))
leftUpPath.addLine(to: CGPoint(x: cutPicture, y: 0))
UIColor(rgb : 0xffffff).setFill()
leftUpPath.fill()
}
let iv = UIImageView(image:im)
iv.frame.origin = CGPoint(x: 0, y: 0)
iv.clipsToBounds = true
let userPicture = UIImageView(image: picture)
userPicture.frame = iv.bounds
userPicture.clipsToBounds = true
iv.addSubview(userPicture)
cardView.addSubview(iv)
我的自定义路径运行良好,但是当我添加 userPicture 时,图像不会超出路径的范围......
这就是我所拥有的:
这就是我想要的:
(用猫图片替换白色背景):
我做错了什么?
【问题讨论】:
-
想添加您当前的结果以及预期结果吗? :)
-
Clip to bounds only 意味着
UIImageView将防止图像“过度填充”视图的边界 -
@Glenn 完成!把白色的bg换成猫图就是我想要的!
标签: ios swift uiimageview uibezierpath