Search code examples
objective-cmacoscocoacore-animation

NSImage not being displayed while using CoreAnimation when there are several images in code?


I am using the following code to display an image on the screen.

The class is a an NSViewController subclass.

-(void) loadView{
       [super loadView];
      CALayer * layer = [ CALayer layer ] ;
        NSImage * image = [[ NSImage alloc ] initWithContentsOfURL:[ NSURL URLWithString:@"http://etc-mysitemyway.s3.amazonaws.com/icons/legacy-previews/icons/glossy-black-icons-sports-hobbies/044450-glossy-black-icon-sports-hobbies-ball-beach.png" ] ] ;
        layer.contents = image ;
        layer.bounds = (CGRect){ .size = { 600, 600 } } ;
        [((NSView*)self.view).layer addSublayer:layer ] ;

}

When I run the code I dont see any image on screen.I want to show this image on the screen.I should also add that there are other images in the nib as well.I want this image on top of all other images.


Solution

  • The layer of your view is nil because you haven't told the view that is wants a layer:

    [self.view setWantsLayer:YES];