I have images stored in an Asset Catalogue and set up to do slicing (9 slicing as I'd call it based on my background). This allows you to use an image with rounded or shaped corners to keep the aspect ratio of the corners if you draw the image at different sizes. The middle of the image gets stretched to accommodate the different size.
I have slicing set up in the Asset Catalogue, and if I use the image in a UIImageView
it works as expected.
However if I try to use the image with a CALayer
by setting the contents it does not work. The image is displayed as you'd expect if you were not using slicing.
I set this up like so:
borderImageLayer = [CALayer layer];
borderImageLayer.contents = (id) _borderImage.CGImage;
borderImageLayer.frame = self.bounds;
[self.layer addSublayer:borderImageLayer];
So, my question is, how do you get CALayer
to work with sliced images? Is it possible?
You're talking about stretchable images, correct? CALayer does not support stretchable images. You would have to build that yourself, and it would be a fair amount of work. Why though? UIImage already supports stretchable images. It's a "solved problem." Just use that.
If you really need to use layers then I would suggest creating a custom subclass of CALayer that sets up 9 sublayers, installs the image tiles as the contents of each sublayer, and responds to resizing by resizing the tiles.