CALayer alpha mask not working

I am trying to create a simple example of drawing an image on a layer, and then set this alpha mask of the layer. I added the code below to the viewDidLoad sample application. without setting the mask, I can see the image of the sheet. after I set the mask, I don’t see anything where my sublevel was. What am I doing wrong?

Here are two images that I use (only samples that I found on the net) http://sorenworlds.netfirms.com/Alpha/leaf.jpg

http://sorenworlds.netfirms.com/Alpha/leaf-alfa.jpg

self.view.layer.backgroundColor = [UIColor orangeColor].CGColor;
self.view.layer.cornerRadius = 20.0;
self.view.layer.frame = CGRectInset(self.view.layer.frame, 20, 20);


CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [UIColor blueColor].CGColor;
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shadowOpacity = 0.8;
sublayer.frame = CGRectMake(30, 30, 128, 192);
[self.view.layer addSublayer:sublayer];

CGImageRef img = [UIImage imageNamed:@"leaf.jpg"].CGImage;
sublayer.borderColor = [UIColor blackColor].CGColor;
sublayer.borderWidth = 2.0;
sublayer.contents = img;

CGImageRef imgAlpha = [UIImage imageNamed:@"leaf_alpha.jpg"].CGImage;   
CALayer *alphaLayer = [CALayer layer];
alphaLayer.contents = (id)imgAlpha;
sublayer.mask = alphaLayer;

Any help is appreciated.

+3
source share
1 answer

There are several problems in your code:

  • CALayer , - . (alphaLayer) JPG, -. , RGB -, , ( ).

    , , 0 , .

    , ​​ Pixelmator, PNG ( ) . .

  • "leaf-alfa.jpg", "leaf_alpha.jpg" .

  • . , . , , -, (, , ). - :

    alphaLayer.bounds = CGRectMake(0, 0, 128, 192);
    alphaLayer.position = CGPointMake(sublayer.bounds.size.width/2.0,
                                      sublayer.bounds.size.height/2.0);
    
+11

All Articles