How to implement alpha gradient on a image?

后端 未结 4 1727
陌清茗
陌清茗 2020-12-13 11:02

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.

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 11:31

    You can try CAGradientLayer.

    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] autorelease];
     CAGradientLayer *gradient = [CAGradientLayer layer];
     gradient.frame = view.bounds;
     gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
     [view.layer insertSublayer:gradient atIndex:0];
    

    Another option

    Try the answer suggested by @Caleb in this previous SO question

    You can go for Graphics Contexts. All drawing happens in a graphics context. If you want to create an image that has a radial gradient, or a linear gradient, or anything else, you'll need to:

    • Create a graphics context for your image with
      UIGraphicsBeginImageContextWithOptions.
    • Do whatever drawing you want to appear in the image.
    • Call UIGraphicsGetImageFromCurrentImageContext to get the image from the context.
      This gives you a UIImage, so no need to convert from a CGImage.
    • Call UIGraphicsEndImageContext to clean up the context.

    You can also have a look at Radial gradient on UImage

提交回复
热议问题