How to render a radial gradient onto a new UIImage

2019-01-26 00:58发布

Just wondering how to render a radial gradient (point > circle) onto a new UIImage (iphone). I saw the following:

http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html

And it made me think i need to use CGShadingRef or CGGradientRef, and use UIImage's 'imageWithCGImage' constructor to go from the CG* to a UIImage... but i can't figure out how.

Any suggestions greatly appreciated!

5条回答
做个烂人
2楼-- · 2019-01-26 01:15

You can also use CoreImage in iOS5+ and use the Vignette Filter.

- (UIImage *)vignetteImageOfSize:(CGSize)size withImage:(UIImage *)image {  
    UIGraphicsBeginImageContextWithOptions(size, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, CGRectMake(0.0, 0.0, size.width, size.height));

    CIImage *coreImage = [CIImage imageWithCGImage:image.CGImage];
    CGPoint origin = [coreImage extent].origin;
    CGAffineTransform translation =
    CGAffineTransformMakeTranslation(-origin.x, -origin.y);
    coreImage = [coreImage imageByApplyingTransform:translation];

    CIFilter *vignette = [[CIFilter filterWithName:@"CIVignette"] retain];
    [vignette setValue:@1.5 forKey:@"inputRadius"];
    [vignette setValue:@1.5 forKey:@"inputIntensity"];
    [vignette setValue:coreImage forKey:@"inputImage"];

    UIImage *vignetteImage = [UIImage imageWithCIImage:vignette.outputImage];
    [vignette release];

    CGRect imageFrame = CGRectMake(0.0, 0.0, size.width, size.height);
    [image drawInRect:imageFrame];
    UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return renderedImage;
}
查看更多
仙女界的扛把子
3楼-- · 2019-01-26 01:23

Set Radial Gradient color for UIView,

FOR GRADIENT,

extension UIColor {
func colorWithRadialGradient(frame: CGRect, colors: [UIColor]) -> UIColor {
    if frame.width != 0.0 && frame.height != 0.0 {
        let backgroundGradientLayer = CAGradientLayer()
        backgroundGradientLayer.frame = frame
        let cgColors = colors.map({$0.cgColor})
        backgroundGradientLayer.colors = cgColors
        backgroundGradientLayer.type = kCAGradientLayerRadial
        backgroundGradientLayer.startPoint = CGPoint(x: 0.5, y: 0.1)
        UIGraphicsBeginImageContext(backgroundGradientLayer.bounds.size)
        backgroundGradientLayer.render(in: UIGraphicsGetCurrentContext()!)
        let backgroundColorImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return UIColor(patternImage: backgroundColorImage!)
    }
    return .clear
    }
}

(Optional) FOR OUTER SHADOW,

extension UIView
{
func setGreenCircularShadow() {
    let greenGradient = [UIColor(red: 13/255.0, green: 160/255.0, blue: 110/255.0, alpha: 0.8),UIColor(red: 7/255.0, green: 200/255.0, blue: 128/255.0, alpha: 1.0),UIColor(red: 7/255.0, green: 200/255.0, blue: 128/255.0, alpha: 1.0), UIColor(red: 13/255.0, green: 160/255.0, blue: 110/255.0, alpha: 0.8)]
    self.backgroundColor        = UIColor().colorWithRadialGradient(frame: self.bounds, colors: greenGradient)
    self.layer.shadowColor      = UIColor(red: 7.0/255.0, green: 200.0/255.0, blue: 128.0/255.0, alpha: 1.0).cgColor
    self.layer.shadowOpacity    = 0.33
    self.layer.shadowOffset     = CGSize(width: 0, height: 5.0)
    self.layer.shadowRadius     = 10
    self.layer.shadowPath       = UIBezierPath(rect: self.bounds).cgPath
   }
}

SET ON UIVIEW,

view.layer.cornerRadius = 5.0
view.setGreenCircularShadow()
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-26 01:35

I wrote this simplyfied method here, (put it into a UIImage category for example)

+ (UIImage *)radialGradientImageWithRadius:(CGFloat)radius StartColor:(UIColor*)startColor EndColor:(UIColor*)endColor ApplyScreenScale:(BOOL)useScreenScale
{
    // Initialize
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(radius * 2, radius * 2), NO, (useScreenScale ? 0.f : 1.f));

    CGContextRef context = UIGraphicsGetCurrentContext();

    // bottom glow gradient
    CGColorSpaceRef colourspace = CGColorSpaceCreateDeviceRGB();

    // build color components
    CGFloat red1 = 0.f, green1 = 0.f, blue1 = 0.f, alpha1 = 0.f;
    [(startColor == nil ? [UIColor clearColor] : startColor) getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
    CGFloat red2 = 0.f, green2 = 0.f, blue2 = 0.f, alpha2 = 0.f;
    [(endColor == nil ? [UIColor clearColor] : endColor) getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2];

    CGFloat cComponents[] = { red1, green1, blue1, alpha1, red2, green2, blue2, alpha2 };
    CGFloat cGlocations[] = { 0.f, 1.f };

    CGGradientRef gradient = CGGradientCreateWithColorComponents(colourspace, cComponents, cGlocations, 2);
    CGPoint centerPoint = CGPointMake(radius, radius);

    CGContextDrawRadialGradient(context, gradient, centerPoint, 0.f, centerPoint, radius , 0.f);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    CGGradientRelease(gradient);
    CGColorSpaceRelease(colourspace);
    UIGraphicsEndImageContext();

    return image;
}

Sample usages:

// resulting image size 128x128 px
UIImage* myRadialImage = [UIImage radialGradientImageWithRadius:128.f StartColor:[UIColor greenColor] EndColor:nil ApplyScreenScale:NO]; 

// resulting image size 256x256 px on normal retina display, or 384x384 on iPhone 6 or gre 
UIImage* myRadialImage = [UIImage radialGradientImageWithRadius:128.f StartColor:[UIColor greenColor] EndColor:[UIColor redColor] ApplyScreenScale:YES]; 

Hope it's useful

查看更多
你好瞎i
5楼-- · 2019-01-26 01:36

You should read about Graphics Contexts in the same document as the section you linked. 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:

查看更多
Fickle 薄情
6楼-- · 2019-01-26 01:38

Ok here's the gist of the working solution, let me know if i missed anything (eg releasing handles / references)

Also posted on my blog: http://splinter.com.au/rendering-a-radial-gradient-on-the-iphone-obj

- (UIImage *)radialGradientImage:(CGSize)size start:(float)start end:(float)end centre:(CGPoint)centre radius:(float)radius {
    // Render a radial background
    // http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html

    // Initialise
    UIGraphicsBeginImageContextWithOptions(size, YES, 1);

    // Create the gradient's colours
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { start,start,start, 1.0,  // Start color
        end,end,end, 1.0 }; // End color

    CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);

    // Normalise the 0-1 ranged inputs to the width of the image
    CGPoint myCentrePoint = CGPointMake(centre.x * size.width, centre.y * size.height);
    float myRadius = MIN(size.width, size.height) * radius;

    // Draw it!
    CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), myGradient, myCentrePoint,
                                 0, myCentrePoint, myRadius,
                                 kCGGradientDrawsAfterEndLocation);

    // Grab it as an autoreleased image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    // Clean up
    CGColorSpaceRelease(myColorspace); // Necessary?
    CGGradientRelease(myGradient); // Necessary?
    UIGraphicsEndImageContext(); // Clean up
    return image;
}
查看更多
登录 后发表回答