Calayer Alpha Mask not working

Source: Internet
Author: User
Tags netfirms

2 down vote favorite 3

I ' m trying to create a simple example of drawing an image to a layer, and then setting that layer ' s alpha mask. I added the code below to my sample app ' s viewdidload. Without setting the mask, I can see the image of the leaf. After I set the mask, I see nothing where my sublayer is. What am I doing wrong?

Here is the images I ' m using (just samples 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 greatly appreciated.

---------------------------------------------------

There is several problems with your code:

  1. The Calayer class reference says that the mask layer's alpha is used to mask the parent layer. Your Mask Layer (Alphalayer) is created with a JPG, which doesn ' t contain alpha values. Quartz doesn ' t understand that's want to use the grayscale RGB pixel values in the mask image as alpha values, and there ' s no code that the can call to does this (as far as I know).

    Quartz probably interprets the lack of alpha in the mask image as 0 alpha for every pixel which are why nothing is Displaye D.

    I suggest getting a paint program, like Pixelmator, and making a PNG file (which have alpha values built in) out of the Ori Ginal leaf image. Doing this removes the need for adding a mask layer in code.

  2. Your file name is "Leaf-alfa.jpg", but it's "leaf_alpha.jpg" in Your code.

  3. Sublayers must is laid-out in their parent layer ' s coordinates. You does not have a boundary for the mask layer. A layer ' s default boundary is the empty rectangle, so even if you used an image with an appropriate alpha mask, you would still see nothing (or maybe just the original leaf image with no masking). You would need to does something like this:

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

Calayer Alpha Mask not working

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.