0

I have a white image with transparent area in the middle like this:

enter image description here

I want to overlay it with colour (non-transparent area only) so it looks like this:

enter image description here

How can I do this?

I have looked into this answer but it overlay the whole image including the non-transparent area.

Community
  • 1
  • 1
imstillalive
  • 369
  • 4
  • 14

2 Answers2

0

So your image is effectively a "mask". Play around with the "CGContextClipToMask" call and the above code that you referenced.

cbiggin
  • 1,942
  • 1
  • 17
  • 18
0

Try the following code and it gives the exact what you want.

    UIGraphicsBeginImageContextWithOptions(imgView.image.size, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextFillRect(context, (CGRect){ {0,0}, imgView.image.size} );

    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, imgView.image.size.height);
    CGContextConcatCTM(context, flipVertical);
    CGContextDrawImage(context, (CGRect){ {0,0}, imgView.image.size }, [imgView.image CGImage]);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [imgView setImage:image];