Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement alpha gradient on a image?

I want to implement alpha gradient on an image. From 0.5 alfa on top of the image to 0.0 on bottom. Any advice, tutorial, link is welcome.

like image 708
Alex Terente Avatar asked May 06 '11 08:05

Alex Terente


People also ask

How do you put a gradient on a picture?

On your image, position the pointer where you would like the gradient to begin. Click and drag over the image in the direction you would like the gradient to follow. Release the mouse button. The gradient is applied to the entire layer.


1 Answers

You can use CGImageCreateWithMask to apply a masking image to it. You could generate an appropriate mask simply enough by drawing to a greyscale or alpha-only CGBitmapContext with CGContextDrawLinearGradient.

If it's being displayed as the content of a CALayer, you could apply an appropriate masking layer to the parent layer's mask property. You could use a CAGradientLayer with appropriate colors to create this mask.

You can draw the image to a CGBitmapContext, and then draw an appropriate alpha gradient over it using kCGBlendModeDestinationIn. Or draw the gradient first, and draw the image over it using kCGBlendModeSourceIn. In both cases, CGContextDrawLinearGradient is again your friend. Then, of course, get the image out of the CGContext using CGBitmapContextCreateImage or CGImageCreate on the underlying data buffer.

Or, of course, if you control the original image and never need a version without the alpha gradient, you could just store it as a PNG with the appropriate alpha values in the first place.

like image 113
Anomie Avatar answered Sep 18 '22 18:09

Anomie