只是想知道如何将径向渐变(点>圆)渲染到新的UIImage(iphone)上。 我看到了以下内容:
它让我觉得我需要使用CGShadingRef或CGGradientRef,并使用UIImage的'imageWithCGImage'构造函数从CG *转到UIImage ......但我无法弄清楚如何。
任何建议都非常感谢!
答案 0 :(得分:10)
好的,这是工作解决方案的要点,让我知道我是否错过任何东西(例如发布句柄/参考)
也发布在我的博客上: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;
}
答案 1 :(得分:2)
您也可以在iOS5 +中使用CoreImage并使用暗角滤镜。
- (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;
}
答案 2 :(得分:1)
您应该在与您关联的部分相同的文档中阅读Graphics Contexts。所有绘图都在图形上下文中进行。如果要创建具有径向渐变,线性渐变或其他任何内容的图像,则需要:
答案 3 :(得分:1)
我在这里写了这个简单的方法,(例如把它放到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];
希望它有用
答案 4 :(得分:0)
设置UIView的“径向渐变”颜色,
对于梯度
var
(可选)“外部阴影”,
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
}
}
在UIVIEW上设置
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
}
}