iosuiimageuicolortintcolor

UIColor Pattern Image and Tint Color


I am working on an app where elements need to be customizable in different colors. Up to this point I have been taking advantage of tintColor and imageWithRenderingMode to change image colors. I am currently in a situation where I need to add a background tile image using UIColor(patternImage:) and backgroundColor. Is there a way to apply a kind of tint to the background tile image so I can change the background image color at runtime?


Solution

  • With the link that I found here this is what I got it to work

    // originalImage and originalColor are defined
    var image = originalImage.imageWithRenderingMode(.AlwaysTemplate)
    UIGraphicsBeginImageContextWithOptions(
        originalImage.size,
        false,
        originalImage.scale)
    originalColor.set()
    image.drawInRect(CGRectMake(
        0,
        0,
        originalImage.size.width,
        originalImage.size.height))
    image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return UIColor(patternImage: image)