I am attempting to draw an image onto a CALayer. I only need an image, so I have created my later as follows:
CALayer *layer = [CALayer layer];
I add my image as follows:
NSImage *img = [[NSImage alloc] initWithContentsOfFile:@"path/to/img.png"];
[layer setContents:img];
This works, however it draws my images to fill the entire parent frame (stretching my image in the process).
Reading the docs, I found the following:
[layer setContentsGravity:@"kCAGravityBottomLeft"];
I am attempting to draw my image in the bottom left of the parent frame, however no matter what I do it draws my icon in the bottom center. Is there anyway to specify the bottom left?
Try this:
CALayer *layer = [CALayer layer];
layer.contentsGravity = kCAGravityBottomLeft;
layer.contents = (id) aCGImageRef;
Note that kCAGravityBottomLeft
is declared as NSString
and the contents
property expects a CGImageRef
.
NSViews
because you can set autoresizing mask - Costique 2012-04-09 18:19
sizeToFit
andsizeThatFits
. But I haven't worked much with images, so this is mostly guesswork - Wienke 2012-04-06 00:21