Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...