Я написал этот простой метод здесь (например, поместите его в категорию UIImage)
+ (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;
}
Примеры использования:
// 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];
Надеюсь, это полезно