简体   繁体   English

CALayer alpha面具无法正常工作

[英]CALayer alpha mask not working

I'm trying to create a simple example of drawing an image to a layer, and then setting that layer's alpha mask. 我正在尝试创建一个将图像绘制到图层,然后设置该图层的alpha蒙版的简单示例。 I added the code below to my sample app's viewDidLoad. 我将下面的代码添加到我的示例应用程序的viewDidLoad中。 without setting the mask, i can see the image of the leaf. 没有设置掩码,我可以看到叶子的图像。 after i set the mask, i see nothing where my sublayer was. 在我设置了面具之后,我看不出我的子层是什么。 what am i doing wrong? 我究竟做错了什么?

Here are the two images i'm using (just samples i found on the net) http://sorenworlds.netfirms.com/Alpha/leaf.jpg 这是我正在使用的两个图像(只是我在网上找到的样本) http://sorenworlds.netfirms.com/Alpha/leaf.jpg

http://sorenworlds.netfirms.com/Alpha/leaf-alfa.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 are several problems with your code: 您的代码有几个问题:

  1. The CALayer class reference says that the mask layer's alpha is used to mask the parent layer. CALayer类引用表示掩码图层的alpha用于掩盖父图层。 Your mask layer (alphaLayer) is created with a JPG, which doesn't contain alpha values. 您的遮罩层(alphaLayer)是使用JPG创建的,该JPG不包含Alpha值。 Quartz doesn't understand that you want to use the grayscale RGB pixel values in the mask image as alpha values, and there's no code that you can call to do this (as far as I know). Quartz不明白你想要将掩码图像中的灰度RGB像素值用作alpha值,并且没有代码可以调用来执行此操作(据我所知)。

    Quartz probably interprets the lack of alpha in the mask image as 0 alpha for every pixel which is why nothing is displayed. Quartz可能会将掩码图像中缺少的alpha解释为每个像素的0 alpha,这就是为什么没有显示的原因。

    I suggest getting a paint program, like Pixelmator, and making a PNG file (which has alpha values built in) out of the original leaf image. 我建议获取一个绘图程序,如Pixelmator,并从原始叶图像中制作一个PNG文件(内置alpha值)。 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. 您的文件名是“leaf-alfa.jpg”,但是代码中的“leaf_alpha.jpg”。

  3. Sublayers must be laid-out in their parent layer's coordinates. 子图层必须在其父图层的坐标中布局。 You did not do this, nor did you set 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). 图层的默认边界是空矩形,因此即使您使用了具有适当alpha遮罩的图像,您仍然看不到任何东西(或者可能只是没有遮罩的原始叶图像)。 You would need to do 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM