[英]Add UIImage in a UIGraphicsImageRenderer
I'm beginner in Swift and I want to add an image to a custom path draw with UIGraphicsImageRenderer
.我是 Swift 的初学者,我想使用UIGraphicsImageRenderer
将图像添加到自定义路径绘制中。 I show you my code:我给你看我的代码:
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)
My custom path work well but when I add the userPicture
the image take not the bounds of the path...我的自定义路径运行良好,但是当我添加userPicture
,图像不占用路径的边界...
This is what I have:这就是我所拥有的:
and this is what I want:这就是我想要的:
(replace the white background by the cat picture): (用猫图代替白色背景):
What am I doing wrong?我究竟做错了什么?
I solved my question with another solution (add the path to a shapeLayer and it to the mask of a UIImageView
):我用另一个解决方案解决了我的问题(将路径添加到 shapeLayer 并将其添加到UIImageView
的掩码):
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addArc(withCenter: CGPoint(x: cornerRadiusView, y: cornerRadiusView),
radius: cornerRadiusView,
startAngle: .pi * 3 / 2,
endAngle: .pi,
clockwise: false)
path.addLine(to: CGPoint(x: 0, y: height))
path.addLine(to: CGPoint(x: newWidth - cutPicture, y: height))
path.addLine(to: CGPoint(x: newWidth, y: 0))
path.addLine(to: CGPoint(x: cutPicture, y: 0))
path.close()
let shapeLayer = CAShapeLayer()
shapeLayer.frame = customShape.bounds
shapeLayer.path = path.cgPath
shapeLayer.fillColor = UIColor.red.cgColor
customShape.layer.mask = shapeLayer;
customShape.layer.masksToBounds = true;
customShape.image = userPicture
customShape.contentMode = .scaleAspectFill
final :最终的 :
A subview's content always appears on top of the content of all of its ancestors.子视图的内容总是出现在其所有祖先的内容之上。 You make userPicture
a subview of iv
, so userPicture
's content appears on top of iv
's content.您将userPicture
iv
的子视图,因此userPicture
的内容出现在iv
的内容之上。 The content of userPicture
is the cat picture. userPicture
的内容是猫的图片。 The content of iv
is the purple frame. iv
的内容是紫色框。 So the cat picture appears on top of the purple frame.所以猫图片出现在紫色框架的顶部。
Instead, make userPicture
the superview, and add a frameView
subview to it containing the purple frame.相反,让userPicture
成为userPicture
视图,并向其中添加一个包含紫色框架的frameView
子视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.