如何在iphone上的新UIImage上渲染径向渐变

问题描述:

只是想知道如何将径向渐变(点>圆)渲染到新的UIImage(iphone)上。
我看到以下内容:

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

它让我觉得我需要使用CGShadingRef或CGGradientRef,并使用UIImage的'imageWithCGImage'构造函数从CG *转到UIImage ...但我无法弄清楚如何。

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.

任何建议都非常感谢!

你应该阅读关于图形上下文与您链接的部分相同的文档。所有绘图都在图形上下文中进行。如果要创建具有径向渐变或线性渐变或其他任何内容的图像,则需要:

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:

  • 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.