简体   繁体   中英

Right way to fit image to CALayer contents in swift programmatically?

I am new to programming.

@IBOutlet weak var myView: UIView!

var l: CALayer { 
    return myView.layer
    }

override func viewDidLoad() {
    super.viewDidLoad()
    setUpLayer()
}

func setUpLayer() {
    l.contents = UIImage(named: "IMG_3682.jpg")?.CGImage
    l.contentsGravity = kCAGravityCenter
}

The image appears way bigger than myView.

I got imges with various sizes and ratios. and I want to fit them into UIView.layer What is the right way to do it?

thanks

删除行:

l.contentsGravity = kCAGravityCenter

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