alecail
alecail

Reputation: 4062

CALayer not rendering its UIImage content

I'm porting a CALayer from OS X to iOS.

I created a single view application, copied the image "body.png" in the bundle, and defined a UIView subclass:

I expect this method to add a layer displaying an image when I touch the screen, but the image is not displayed, only the frame is displayed.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch * aTouch = [touches anyObject];
CGPoint loc = [aTouch locationInView:self];
CGPoint p=loc;
CALayer * body = [CALayer layer];
UIImage * img =[UIImage imageNamed:@"body.png"];
[body setBounds:CGRectMake(0, 0, 200, 200)];
[body setPosition:p];

if (img) {
    NSLog(@"body.png loaded");
        [body setBorderWidth:1];
        [body setBorderColor:[[UIColor blackColor] CGColor]];
        [body setContents:(id)img];
    }
    else {
        [body setBackgroundColor:[[UIColor blueColor] CGColor]];
    NSLog(@"body.png not loaded");
    }

    [[self layer] addSublayer:body];
}

What am I missing ?

Upvotes: 2

Views: 540

Answers (1)

Matti Jokipii
Matti Jokipii

Reputation: 589

Change the line

[body setContents:(id)img];

to

[body setContents:(id)img.CGImage];

Upvotes: 2

Related Questions