Geethanjali Reddy
Geethanjali Reddy

Reputation: 223

Add and Center image inside CALayer

I want to center the image inside a CALayer. Even the image size must be modified accordingly and color of image should also be tinted. The problem is Im not able to set the image size and even add color.

    //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)

I want to achieve something like below: image

Upvotes: 1

Views: 1838

Answers (1)

Pradeep Kashyap
Pradeep Kashyap

Reputation: 961

Try This one its working :::

  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)

Upvotes: 1

Related Questions