CALayer alpha mask not working


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 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-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. Your mask layer (alphaLayer) is created with a JPG, which doesn‘t contain alpha values. 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 probably interprets the lack of alpha in the mask image as 0 alpha for every pixel which is why nothing is displayed.

    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. 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 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). 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);
    
时间: 2024-10-01 07:47:10

CALayer alpha mask not working的相关文章

关于使用 CALayer 中 mask 的一些技巧

CALayer 拥有 mask 属性,Apple 的官方解释如下: An optional layer whose alpha channel is used to mask the layer's content. The layer's alpha channel determines how much of the layer's content and background shows through. Fully or partially opaque pixels allow the

CALayer的mask属性

可以对图层按path进行指定裁剪 //#import "ViewController.h" // //@interface ViewController () // //@end // //@implementation ViewController // //- (void)viewDidLoad { // // [super viewDidLoad]; // // // 创建一个蓝色的Layer // CALayer *foregroundLayer = [CALayer laye

ImageMagick 拆分透明PNG、合并JPG和Alpha Mask

透明PNG图片占用空间较大,可以拆分为JPG图片和只带Alpha的PNG图片.这里使用ImageMagick进行拆分和合并实验. ImageMagick版本:6.9.0-Q16 以Bird.png图片进行测试: 拆分透明PNG为JPG和只带Alpha的PNG 1 2 "D:\Program Files\ImageMagick-6.9.0-Q16\convert.exe" Bird.png -background black -alpha remove Bird.jpg "D:

iOS Core Animation之CALayer心得

使用CALayer的mask实现注水动画效果 Core Animation一直是iOS比较有意思的一个主题,使用Core Animation可以实现非常平滑的炫酷动画.Core animtion的API是较高级的封装,使用便捷,使得我们免于自己使用OpenGL实现动画.本文主要介绍如何使用CALayer的mask实现一个双向注水动画(姑且这么叫吧). 了解CALayer的mask 以上是CALayer的头文件关于mask的说明,mask实际上layer内容的一个遮罩. 如果我们把mask是透明的

自己总结的 iOS ,Mac 开源项目以及库,知识点------持续更新

自己在 git  上看到一个非常好的总结的东西,但是呢, fork  了几次,就是 fork  不到我的 git 上,干脆复制进去,但是,也是认真去每一个每一个去认真看了,并且也是补充了一些,感觉非常棒,所以好东西要分享,为啥用 CN 博客,有个好处,可以随时修改,可以持续更新,不用每次都要再发表,感觉这样棒棒的 我们 自己总结的iOS.mac开源项目及库,持续更新.... github排名 https://github.com/trending,github搜索:https://github.

iOS开发 非常全的三方库、插件、大牛博客等等

UI 下拉刷新 EGOTableViewPullRefresh- 最早的下拉刷新控件. SVPullToRefresh- 下拉刷新控件. MJRefresh- 仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能.可以自定义上下拉刷新的文字说明.具体使用看"使用方法". (国人写) XHRefreshControl- XHRefreshControl 是一款高扩展性.低耦合度的下拉刷新.上提加载更多的组件.(国人写) CBStoreHo

iOS、mac开源项目及库汇总

UI 下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能.可以自定义上下拉刷新的文字说明.具体使用看“使用方法”. (国人写) XHRefreshControl – XHRefreshControl 是一款高扩展性.低耦合度的下拉刷新.上提加载更多的组件.(国人写) CBStoreHou

最全面的iOS和Mac开源项目和第三方库汇总

UI 下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能.可以自定义上下拉刷新的文字说明.具体使用看“使用方法”. (国人写) XHRefreshControl – XHRefreshControl 是一款高扩展性.低耦合度的下拉刷新.上提加载更多的组件.(国人写) CBStoreHou

iOS开发之资料收集

github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自github:https://github.com/Tim9Liu9/TimLiu-iOS UI 下拉刷新 EGOTableViewPullRefresh- 最早的下拉刷新控件. SVPullToRefresh- 下拉刷新控件. MJRefresh- 仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者