[英]Swift: Add image to CALayer returns black
Very simply, I'm trying to add an image to a CALayer as a watermark on the video.很简单,我正在尝试将图像添加到 CALayer 作为视频上的水印。 I have done this on projects in the past and it has worked but for some reason it's consistently returning a black image on my current project.
我过去曾在项目中这样做过,并且它确实有效,但由于某种原因,它一直在我当前的项目中返回黑色图像。
Here is code:这是代码:
I've unwrapped 'logo' to ensure it is valid我已经打开了“标志”以确保它是有效的
I've set the background to green.我已将背景设置为绿色。 Currently the frame is filled black (not green), so I assume it is returning the image, but rendering black for some reason.
目前该帧被填充为黑色(不是绿色),所以我认为它正在返回图像,但由于某种原因呈现黑色。
if let logo = options.logoImage { let layer = CALayer() animationLayer.addSublayer(layer) layer.frame = CGRect(x: 0, y: 0, width: 300, height: 300) layer.backgroundColor = UIColor.green.cgColor layer.contents = logo.cgImage layer.contentsGravity =.resizeAspect }
I think you forgot to set your layer to the mask property:我认为您忘记将图层设置为遮罩属性:
layer.mask = imageSubLayer // or whatever it is called
So it would look something like this:所以它看起来像这样:
if let logo = options.logoImage {
let layer = CALayer()
animationLayer.addSublayer(layer)
layer.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
layer.backgroundColor = UIColor.green.cgColor
layer.contents = logo.cgImage
layer.mask = animationLayer
layer.contentsGravity = .resizeAspect
}
Edit:编辑:
CALayer `s are very transient and doesn't work properly outside of animations: CALayer非常短暂,在动画之外不能正常工作:
I tried to reproduce what you did (I just assumed you created your animationLayer the same I did):我试图重现您所做的事情(我只是假设您创建了与我相同的animationLayer ):
var animationLayer: CALayer = {
let l = CALayer()
l.contentsGravity = kCAGravityResizeAspect
return l
}()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.