Skip to content

Instantly share code, notes, and snippets.

@tonyarnold
Last active November 17, 2019 23:13
Show Gist options
  • Save tonyarnold/826d1140c8b6c5c522d5 to your computer and use it in GitHub Desktop.
Save tonyarnold/826d1140c8b6c5c522d5 to your computer and use it in GitHub Desktop.
Am I misunderstanding how CALayer's contentsCenter property works?
NSEdgeInsets capInsets = backgroundImage.capInsets;
NSSize imageSize = backgroundImage.size;

CALayer *layer = someView.layer;
layer.contents = backgroundImage;
layer.contentsScale = self.backingScaleFactor;
if (someView.isFlipped)
{
    layer.contentsCenter = CGRectMake(
        capInsets.left / imageSize.width,
        capInsets.top / imageSize.height,
        1.0 - (capInsets.right / imageSize.width),
        1.0 - (capInsets.bottom / imageSize.height));
}
else
{
    layer.contentsCenter = CGRectMake(
        capInsets.left / imageSize.width,
        capInsets.bottom / imageSize.height,
        1.0 - (capInsets.right / imageSize.width),
        1.0 - (capInsets.top / imageSize.height));
}
**** Misuse of NSImage and CALayer. contentsCenter is {{0.037037037037037035, 0.37931034482758619}, {0.96296296296296302, 0.48275862068965514}}. It should be {{0, 0}, {1, 1}}

U wot mate?

@Kentzo
Copy link

Kentzo commented Nov 11, 2019

Ever found an explanation?

@tonyarnold
Copy link
Author

@Kentzo, sorry but this was years ago. Are you seeing this problem somewhere?

@Kentzo
Copy link

Kentzo commented Nov 17, 2019

Nope. Asked in case you figured it out so I could avoid it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment