在CALayer中添加和居中图像

时间:2018-02-22 07:17:08

标签: swift uiimage calayer

我想将图像置于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)

我想实现以下目标: image

1 个答案:

答案 0 :(得分:0)

尝试这个工作:::

  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)