Drawing UIImage in CALayer.contents does not display image data

I have evil times trying to get the image that appeared in CALayer.contents. It seems pretty straight forward, but I can't get the image to render no matter what I do. CALayermakes it thin because I can see its background color and radius of the corner, but the image does not load.

enter image description here

What you see here is a subclass CAGradientLayerwith the mask applied. The inner square is where I would like to show the image, and it is added as a subclass of the subclass CAGradientLayer.

The installation code is quite simple. In init:

self.imageLayer = [CALayer layer];
self.imageLayer.cornerRadius = kDefaultCornerRadius;
self.imageLayer.backgroundColor = [UIColor darkGrayColor].CGColor;
[self addSublayer:self.imageLayer];

Then later 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 checked:

  • , , , .
  • . [UIImage imageNamed:@"info.png"] . 16x16 1x 32x32 2x
  • : init, setImage:, setFrame;

?

+5
1

:

[self.imageLayer setNeedsDisplay];

-setNeedsDisplay , . , , CA .

+13

All Articles