如何在iphone上的新UIImage上渲染径向渐变 [英] How to render a radial gradient onto a new UIImage on an iphone
问题描述
只是想知道如何将径向渐变(点>圆)渲染到新的UIImage(iphone)上。
我看到以下内容:
Just wondering how to render a radial gradient (point > circle) onto a new UIImage (iphone). I saw the following:
它让我觉得我需要使用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:
- 使用 UIGraphicsBeginImageContextWithOptions 。
- 执行您想要在图像中显示的任何图形。
- 致电 UIGraphicsGetImageFromCurrentImageContext 从上下文中获取图像。这给你一个UIImage,所以不需要从CGImage转换。
- Call UIGraphicsEndImageContext 来清理上下文。
- 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.
这篇关于如何在iphone上的新UIImage上渲染径向渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!