zoukankan      html  css  js  c++  java
  • 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. 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);
      
  • 相关阅读:
    Linux服务器管理: 系统的定时任务crond
    Nmon的安装使用及获取报表
    笔记:LoadRunner性能测试巧匠训练营
    python-解决安装MySQL-python出现的: Python version 2.7 required,which was not found in the registry
    JMeter监控内存及CPU——plugin插件监控被测系统资源方法
    Linux监控
    SSL与TLS的区别以及介绍
    [存]Jmeter 如何实现跨线程组传递参数
    Robot Framework简介
    [转]Appium搭建六:安装Android模拟器
  • 原文地址:https://www.cnblogs.com/allanliu/p/4207738.html
Copyright © 2011-2022 走看看