Note that there are some explanatory texts on larger screens.

plurals
  1. POHow would I tint an image programmatically on the iPhone?
    text
    copied!<p>I would like to tint an image with a color reference. The results should look like the Multiply blending mode in Photoshop, where <em>whites</em> would be replaced with <em>tint</em>:</p> <p><img src="https://i.stack.imgur.com/CqiUz.png" alt="alt text"></p> <p>I will be changing the color value continuously.</p> <p><strong>Follow up:</strong> I would put the code to do this in my ImageView's drawRect: method, right?</p> <p>As always, a <em>code snippet</em> would greatly aid in my understanding, as opposed to a link.</p> <p><strong>Update:</strong> Subclassing a UIImageView with the code <strong>Ramin</strong> suggested. </p> <p>I put this in viewDidLoad: of my view controller:</p> <pre><code>[self.lena setImage:[UIImage imageNamed:kImageName]]; [self.lena setOverlayColor:[UIColor blueColor]]; [super viewDidLoad]; </code></pre> <p>I see the image, but it is not being tinted. I also tried loading other images, setting the image in IB, and calling setNeedsDisplay: in my view controller.</p> <p><strong>Update</strong>: drawRect: is not being called.</p> <p><strong>Final update:</strong> I found an old project that had an imageView set up properly so I could test Ramin's code and it works like a charm!</p> <p><strong>Final, final update:</strong></p> <p>For those of you just learning about Core Graphics, here is the simplest thing that could possibly work.</p> <p>In your subclassed UIView:</p> <pre><code>- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColor(context, CGColorGetComponents([UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:1].CGColor)); // don't make color too saturated CGContextFillRect(context, rect); // draw base [[UIImage imageNamed:@"someImage.png"] drawInRect: rect blendMode:kCGBlendModeOverlay alpha:1.0]; // draw image } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload