tags:

views:

508

answers:

2

According to the Mac Dev Center docs, you should be able to set the contents property of a CALayer and have that render automatically. However, I still can't get a simple image to show up by adding a sublayer to the UIView's root later. I've tried multiple different variations; here's what I have so far:

(Note: I know there are other ways of rendering images; for my purposes I'd like to use CALayer's for some of the more complicated stuff I'm going to get into).

(in viewDidDisplay() of the ViewController):

 CALayer *theLayer = [CALayer layer];
 [[[self view] layer] addSublayer:theLayer];
 theLayer.contents = (id)[[UIImage imageNamed:@"mypic.png"] CGImage];
 theLayer.contentsRect = CGRectMake(0.0f, 0.0f, 300.0f, 300.0f);
 theLayer.bounds = CGRectMake(0.0f, 0.0f, 300.0f, 400.0f);

Anyone know what I'm doing wrong?

Thanks!

A: 

You don't need to set the contentsRect (and if you do, it should be in the unit coordinate space, probably just CGRectMake(0, 0, 1.0, 1.0).

You might want to set the layer's position property.

Ben Gottlieb
A: 

You could load the image into a UIImageView, which is decended from UIView and therefore has it's own layer property.

UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
imgView.image = [UIImage imageNamed:@"mypic.png"];

[[[self view] layer] addSublayer:[imgView layer]];
[imgView release];
imnk