Chr*_*ris 2 iphone gradient objective-c radial-gradients uiimage
只是想知道如何将径向渐变(点>圆)渲染到新的UIImage(iphone)上.我看到以下内容:
它让我觉得我需要使用CGShadingRef或CGGradientRef,并使用UIImage的'imageWithCGImage'构造函数从CG*转到UIImage ...但我无法弄清楚如何.
任何建议都非常感谢!
Chr*_*ris 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11131 次 |
| 最近记录: |