I am having a wicked time trying to get an image to show up in a CALayer.contents
. It seems rather straight forward but I cannot get the image to render no matter what I do. The CALayer
renders fine as I can see its background color and corner radius, but the image will not load.
What you are seeing here is a CAGradientLayer
subclass with a mask applied. The inner square is where I would like the image to show, and it is added as a sublayer of the CAGradientLayer
subclass.
The code to set this up is pretty straightforward. In init
:
self.imageLayer = [CALayer layer];
self.imageLayer.cornerRadius = kDefaultCornerRadius;
self.imageLayer.backgroundColor = [UIColor darkGrayColor].CGColor;
[self addSublayer:self.imageLayer];
Then later on, I set the image:
- (void)setImage:(UIImage *)image {
self.imageLayer.contents = (id)image.CGImage;
[self.imageLayer setNeedsDisplay];
}
Finally, within setFrame
:
CGFloat imageSize = self.bounds.size.width - 2*kDefaultMargin;
[self.imageLayer setBounds:CGRectMake(0.0, 0.0, imageSize, imageSize)];
[self.imageLayer setPosition:CGPointMake(self.bounds.size.width/2.0f, kDefaultMargin + imageSize/2.0f)];
[self.imageLayer setNeedsDisplay];
Things I already know or have checked:
[UIImage imageNamed:@"info.png"]
is being used elsewhere in the code and actually shows and image. It is 16x16 at 1x and 32x32 at 2xinit
, setImage:
, setFrame
;What is going on here?
An object that manages image-based content and allows you to perform animations on that content.
Remove this line:
[self.imageLayer setNeedsDisplay];
-setNeedsDisplay
tells a layer that it needs to redraw its contents. Since you already provided the content for the layer, you don't want CA to discard that content and ask for a replacement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With