CashApelayer图像未出现
我有一个image.cgimage
,我将其设置为contents
cashapelayer()
,但没有出现。我强迫拆开图像,所以我知道它肯定存在。出现圆形,但其中的图像却没有。
let shapeLayer = CAShapeLayer()
var didSubviewsLayout = false
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if !didSubviewsLayout {
didSubviewsLayout = true
setupShapeLayer()
}
}
func setupShapeLayer() {
shapeLayer.path = UIBezierPath(roundedRect: CGRect(x: 200, y: 200, width: 50, height: 50), cornerRadius: 25).cgPath
shapeLayer.strokeColor = nil
shapeLayer.fillColor = UIColor.orange.cgColor
shapeLayer.contents = UIImage(named: "myIcon")!.cgImage // this doesn't crash so the image definitely exists
view.layer.addSublayer(shapeLayer)
}
I have an image.cgImage
that I set as the contents
to a CAShapeLayer()
but it's not appearing. I force unwrap the image so I know that it definitely exists. The round shape appears but the image inside of it doesn't.
let shapeLayer = CAShapeLayer()
var didSubviewsLayout = false
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if !didSubviewsLayout {
didSubviewsLayout = true
setupShapeLayer()
}
}
func setupShapeLayer() {
shapeLayer.path = UIBezierPath(roundedRect: CGRect(x: 200, y: 200, width: 50, height: 50), cornerRadius: 25).cgPath
shapeLayer.strokeColor = nil
shapeLayer.fillColor = UIColor.orange.cgColor
shapeLayer.contents = UIImage(named: "myIcon")!.cgImage // this doesn't crash so the image definitely exists
view.layer.addSublayer(shapeLayer)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的形状层没有大小。您没有给它一个框架,因此它只是右上角的一个看不见的点。不幸的是,您仍然可以看到形状,因此您不知道自己做错了这一切。但是内容没有绘制,因为该层只是一个点,因此它揭示了您的代码的粘土脚。
因此,在这种情况下,偶然地更改
为
不是如何正确绘制圆圈,因此,如果这是您的目标,请停止它。使用
ovalin
;就是这样。完整示例:结果:
也请注意,您可能正在用 all 您的形状层犯此错误,而不仅仅是此错误,因此您将需要返回所有此应用程序中的代码(或者更好,您编写的所有代码!),并正确编写所有形状层。图层需要框架!
The problem is that your shape layer has no size. You have not given it a frame, so it is just an invisible dot at the top right corner. Unfortunately, you can still see the shape, so you're not aware of the fact that you're doing this all wrong. But the content doesn't draw, because the layer is just a dot, so it reveals your code's feet of clay.
So, in this case, change
To
Incidentally, that is not how to draw a circle correctly, so if that's your goal, stop it. Use
ovalIn
; that's what it's for. Complete example:Result:
Note too that you are probably making this mistake with all your shape layers, not just this one, so you will want to go back through all the code in this app (or better, all the code you've ever written!) and write all your shape layers correctly. Layers need frames!
您可以在单独的一层中创建圆形路径,并将其添加为掩码,并将其添加到包含图像的图层中。首先,我将在父层中添加一个帧,以便可以使用
contentsGravity
将图像居中。然后,创建将具有圆形路径的图层并将其设置为图像层的掩码:
您的修改代码:
You could create the rounded path in a separate layer and add it as a mask to the layer that contains the image. Firstly, I would add a frame to the parent layer so that the image can be centred within the layer using
contentsGravity
.Then, create the layer that would have the rounded path and set it as a mask to the image layer:
Your modified code: