简体   繁体   中英

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: 图片

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM