我想将图像居中放置在CALayer中。即使图像尺寸也必须相应地修改,并且图像的颜色也应着色。问题是我无法设置图像大小,甚至无法添加颜色。

    //premium gradient
    let premiumViewLayer=CALayer()
    premiumViewLayer.bounds=CGRect(x:0, y:0, width:(getGradientWidth()), height:(LKView.bounds.height))
    premiumViewLayer.backgroundColor = UIColor.clear.cgColor

    let premiumimageViewLayer=CALayer()
    premiumimageViewLayer.frame = SSView.frame
    premiumimageViewLayer.contents=UIImage(named: "premium")?.cgImage
    premiumimageViewLayer.contentsGravity = kCAGravityCenter

    premiumViewLayer.addSublayer(premiumimageViewLayer)


我想要实现以下目标:
swift - 在CALayer中添加图像并居中-LMLPHP

最佳答案

试试这一个其工作:::

  let premiumViewLayer=CALayer()
    premiumViewLayer.frame = CGRect(origin: CGPoint.zero, size: imgView.frame.size)//CGRect(x:0, y:0, width:375, height:333.5)
    premiumViewLayer.backgroundColor = UIColor(white: 1.0, alpha: 0.5).cgColor
    premiumViewLayer.contents=UIImage(named: "premium")?.cgImage
    premiumViewLayer.contentsGravity = kCAGravityCenter
    view(on which on want to add this layer).layer.addSublayer(premiumViewLayer)

07-28 07:41