I have a CGImage that I want to show in a CALayer. Setting the content and the gravity to the bottom-left works like expected.
layer.contents = cgimage
layer.contentsGravity = .bottomLeft
Now I want center and cover the full layer. Using .center just shifts the content center to the bottom-left.
layer.contents = cgimage
layer.contentsGravity = .center
Only when I set the position to the center of the view it displays as expected.
layer.contents = cgimage
layer.contentsGravity = .center
layer.position = CGPoint(x: view.frame.width/2, y: view.frame.height/2)
But with gravity set to resize-aspect-fill nothing is shown at all - with the position set or not.
layer.contents = cgimage
layer.contentsGravity = .resizeAspectFill
I have the feeling that I missing something fundamental.
Why isn't the CGImage in the CALayer covering the full size and keeping the aspect ratio?



Turns out for a
contentGravityof type.resizeand.resizeAspectFillthe frame of aCALayerhas to be explicitly set. For a.centeror.bottomLeftyou can get away without.