Search code examples
objective-cuiimagemask

How can I change every color in a UIImage to gray?


I'm trying to create a mask from an image with a transparent background. I want to create a new UIImage where any pixel that has > 0 alpha to turn gray.


Solution

  • You can load an image with rendering mode UIImageRenderingModeAlwaysTemplate which draws the image as a template image, ignoring its color information.

    [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    

    Then set the tintColor of the container to gray

    yourImageView.tintColor = [UIColor grayColor];