【发布时间】:2015-08-06 08:16:50
【问题描述】:
我想在运行时将白色图标着色为另一种颜色,我尝试使用方法taken from here,但没有成功:
func maskImageView() {
var maskImageSize = CGSizeMake(self.downloadImageView.frame.width, self.downloadImageView.frame.height)
UIGraphicsBeginImageContextWithOptions(maskImageSize, false, 0.0)
var color = UIColor(white: 1.0, alpha: 1.0)
color.setFill()
var rect = CGRectMake(0, 0, self.downloadImageView.frame.width, self.downloadImageView.frame.height)
UIRectFill(rect)
color = BrandColors.BRAND_FIRST_COLOR
color.setFill()
rect = CGRectMake((self.downloadImageView.frame.width/2)-100, (self.downloadImageView.frame.height/2)-100, 200, 200)
UIRectFill(rect)
var maskImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
var maskLayer = CALayer()
maskLayer.frame = CGRectMake(0, 0, self.downloadImageView.bounds.width, self.downloadImageView.bounds.height)
maskLayer.contents = maskImage.CGImage
maskLayer.contentsRect = CGRectMake(0, 0, self.downloadImageView.bounds.width, self.downloadImageView.bounds.height)
self.downloadImageView.layer.mask = maskLayer;
}
我实际上无法弄清楚这个掩蔽是如何工作的。我做错了什么?
【问题讨论】:
-
你的图片里面有透明的部分吗?
-
@DánielNagy 是的。
-
我认为与您找到的解决方案相同:)
标签: ios swift uiimageview uiimage mask