Q.u.a.n.g L.
Q.u.a.n.g L.

Reputation: 1614

How to tint the non-transparent part of an image in iOS with an image?

Does anyone know how to tint the part of non-transparent image and preserve the trans one? Could you maek an instruction to me please? Thanks

Edit: The image has a transparent area in the center and white border (alpha 0.8). I only want to fill the white part with an image (and keep its alpha and the transparent one). In this situation, I think I should use a mask image then mix it with the image I already have.

+(UIImage *)createImageFromImageA:(UIImage *)imgA andImageMask:(UIImage *)imgB{

CGImageRef maskRef = imgB.CGImage;

CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                    CGImageGetHeight(maskRef),
                                    CGImageGetBitsPerComponent(maskRef),
                                    CGImageGetBitsPerPixel(maskRef),
                                    CGImageGetBytesPerRow(maskRef),
                                    CGImageGetDataProvider(maskRef), NULL, false);

CGImageRef masked = CGImageCreateWithMask([imgA CGImage], mask);
return [UIImage imageWithCGImage:masked];
}

It seem that, the output image is the same with the old one, instead of hold image's alpha has been changed. It's blurry. So, is there any problem with my solution?

Upvotes: 0

Views: 375

Answers (1)

Caleb
Caleb

Reputation: 124997

There are several Core Image filters that might be useful, depending on your needs. For the situation you describe in your comment (white frame, transparent center), take a look at CIMultiplyCompositing. This filter multiplies the color components of two images. If you want to change the color of the white frame to, say, red, you could composite your frame image with a solid orange image; the product of solid white (RGBA=1,1,1,1) and solid red (RGBA=1,0,0,1) should be solid red. Transparent pixels should stay transparent.

If you want to do other things, like apply a blur, you'd use a different filter. There are many to choose from, and you can read about them all in the Core Image filter reference linked above.

Upvotes: 1

Related Questions