如何将Swift中的白色图像着色为另一种颜色?

时间:2015-08-06 08:16:50

标签: ios swift uiimageview uiimage mask

我想在运行时将白色图标换成另一种颜色,我试图使用方法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;
}

我实际上无法弄清楚这个掩盖的东西是如何工作的。我做错了什么?

4 个答案:

答案 0 :(得分:8)

我能够使用以下方法完成此任务:

func maskDownloadImageView() {
    downloadImageView.image = downloadImageView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    downloadImageView.tintColor = BrandColors.BRAND_FIRST_COLOR  
}

无需使用遮罩。

答案 1 :(得分:0)

试试这个。

func fillImage(image : UIImage! ,withColor color : UIColor!) -> UIImage!
{
    // Create the proper sized rect
    let imageRect = CGRectMake(0, 0, image.size.width, image.size.height)

    // Create a new bitmap context
    let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
    let context = CGBitmapContextCreate(nil, Int(imageRect.size.width), Int(imageRect.size.height), 8, 0, colorSpace, bitmapInfo)

    // Use the passed in image as a clipping mask
    CGContextClipToMask(context, imageRect, image.CGImage)
    // Set the fill color
    CGContextSetFillColorWithColor(context, color.CGColor)
    // Fill with color
    CGContextFillRect(context, imageRect)

    // Generate a new image
    let newCGImage = CGBitmapContextCreateImage(context)
    let newImage = UIImage(CGImage: newCGImage)


    return newImage;
}

答案 2 :(得分:0)

要屏蔽透明图像,请尝试以下操作:

func maskImageWithColor(color : UIColor!) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
    if let context: CGContextRef = UIGraphicsGetCurrentContext() {
        CGContextTranslateCTM(context, 0, self.size.height)
        CGContextScaleCTM(context, 1.0, -1.0)

        CGContextSetBlendMode(context, .Normal)
        let rect: CGRect = CGRectMake(0, 0, self.size.width, self.size.height)

        // To use gradient, add CGColor to Array

        let colors: [CGColor] = [color.CGColor]
        let colorsPointer = UnsafeMutablePointer<UnsafePointer<Void>>(colors)
        if let colorsCFArray = CFArrayCreate(nil, colorsPointer, colors.count, nil) {
            if let space: CGColorSpaceRef = CGColorSpaceCreateDeviceRGB() {
                if let gradient: CGGradientRef = CGGradientCreateWithColors(space, colorsCFArray, nil) {
                    // Apply gradient
                    CGContextClipToMask(context, rect, self.CGImage)
                    CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0, self.size.height), .DrawsBeforeStartLocation)
                    let coloredImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
                    UIGraphicsEndImageContext()

                    return coloredImage;
                }
            }
        }
    }
    return self
}

您可以使用渐变只在阵列中添加更多CGColor

答案 3 :(得分:0)

迅速的Emil Adz 4版本回答了懒惰。

func maskDownloadImageView() {
    downloadImageView.image = downloadImageView.image?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
    downloadImageView.tintColor = BrandColors.BRAND_FIRST_COLOR
}