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.
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.