在UIImageView上添加0.3不透明度的黑色叠加层

问题描述:

我有一个UIImageView,我想在它上面添加一个黑色覆盖。在不必覆盖drawRect的情况下,这样做的最佳方法是什么?我想在它上面添加一个CALayer。但不确定如何获得一个带有.3 alpha的黑色CALayer。

I have a UIImageView and I wanted to add a black overlay on top of it. What is the best way of doing this without having to override drawRect? I was thinking of adding a CALayer on top of it. But unsure how to get a black CALayer with a .3 alpha.

这样的东西?

UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, myImageView.frame.size.width, myImageView.frame.size.height / 2)];
[overlay setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];
[myImageView addSubview:overlay];