简体   繁体   English

使用CALayer和UIImage在Swift中屏蔽图像

[英]Masking an image in Swift using CALayer and UIImage

I'm programming in Swift. 我在Swift编程。 I want to mask an image using CALayer and UIImage. 我想使用CALayer和UIImage来屏蔽图像。 I'm creating my mask image programmatically. 我正在以编程方式创建我的蒙版图像。 The created mask image is a UIImage and works fine when I view it on its own. 创建的蒙版图像是一个UIImage,当我自己查看它时工作正常。 But when I use it as a mask the whole screen becomes white. 但当我用它作为面具时,整个屏幕变成白色。 I suspect that my problem is in configuring the CALayer object. 我怀疑我的问题在于配置CALayer对象。 I would appreciate your help. 我很感激你的帮助。 Thanks! 谢谢!

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var maskImageSize = CGSizeMake(self.imageView.frame.width, self.imageView.frame.height)
        UIGraphicsBeginImageContextWithOptions(maskImageSize, false, 0.0)

        var color = UIColor(white: 1.0, alpha: 1.0)
        color.setFill()
        var rect = CGRectMake(0, 0, self.imageView.frame.width, self.imageView.frame.height)
        UIRectFill(rect)

        color = UIColor(white: 0.0, alpha: 1.0)
        color.setFill()
        rect = CGRectMake((self.imageView.frame.width/2)-100, (self.imageView.frame.height/2)-100, 200, 200)
        UIRectFill(rect)

        var maskImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        var maskLayer = CALayer()
        maskLayer.contents = maskImage
        maskLayer.contentsRect = CGRectMake(0, 0, self.imageView.bounds.width, self.imageView.bounds.height)


        self.imageView.image = UIImage(named: "pictobemasked.png")

        self.imageView.layer.mask = maskLayer;
    }

}

Unfortunately you've asked your question rather badly - you have not said what it is that you are actually trying to do! 不幸的是,你问的问题相当糟糕 - 你还没有说出你真正想要的是什么! It looks, however, as if you might be trying to punch a rectangular hole in your image view using a mask. 但是,看起来好像你可能试图使用蒙版在图像视图中打出一个矩形孔。 If so, your code has at least three huge flaws. 如果是这样,您的代码至少有三个巨大的缺陷。

  • One reason your code is not working is that a mask is based on transparency, not on color. 您的代码不起作用的一个原因是掩码基于透明度而不是颜色。 You are using an opaque white and an opaque black, which are both opaque, so there is no difference there. 你使用的是不透明的白色和不透明的黑色,它们都是不透明的,所以没有区别。 You need your two colors to be like this: 你需要两种颜色:

      var color = UIColor(white: 1.0, alpha: 1.0) // ... and then, later ... color = UIColor(white: 1.0, alpha: 0.0) 
  • The second problem is that your layer has no size. 第二个问题是您的图层没有大小。 You need to give it one: 你需要给它一个:

     var maskLayer = CALayer() maskLayer.frame = CGRectMake( 0, 0, self.imageView.bounds.width, self.imageView.bounds.height) 
  • The third and biggest problem is that your mask image is never getting into your mask layer, because you have forgotten to extract its CGImage : 第三个也是最大的问题是你的面具图像永远不会进入你的面具层,因为你忘了提取它的CGImage

     maskLayer.contents = maskImage.CGImage 

That last one is really the killer, because if you set the contents to a UIImage without extracting its CGImage , the image fails silently to get into the layer. 最后一个真的是杀手,因为如果你在没有提取CGImage情况下将contents设置为UIImage,那么图像会无声地进入图层。 There is no error message, no crash - and no image. 没有错误消息,没有崩溃 - 也没有图像。

Making those three corrections in your code, I was able to make the mask punch a rectangular hole in an image. 在你的代码中进行了这三个修正,我能够使掩模在图像中打出一个矩形孔。 So if that's your purpose, those changes will achieve it. 因此,如果这是您的目的,那么这些变化将实现它。

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

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