如何编程UIImage? [英] How can I make an UIImage programatically?
问题描述
这不是你可能认为是开始。我知道如何使用UIImage的,但我现在需要知道如何创建一个空白UIImage使用:
This isn't what you probably thought it was to begin with. I know how to use UIImage's, but I now need to know how to create a "blank" UIImage using:
CGRect screenRect = [self。 view bounds];
好吧,那些维度。无论如何,我想知道如何可以创建一个UIImage与那些尺寸着色所有白色。这里没有实际的图片。
Well, those dimensions. Anyway, I want to know how I can create a UIImage with those dimensions colored all white. No actual images here.
这是可能吗?
这需要是一个白色 图片。不是空白的。 :)
This needs to be a "white" image. Not a blank one. :)
推荐答案
您需要使用 CoreGraphics
/ p>
You need to use CoreGraphics
, as follows.
CGSize size = CGSizeMake(desiredWidth, desiredHeight);
UIGraphicsBeginImageContextWithOptions(size, YES, 0);
[[UIColor whiteColor] setFill];
UIRectFill(CGRectMake(0, 0, size.width, size.height));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
代码创建一个新的 CoreGraphics
其中选项作为参数传递;尺寸,不透明度和尺度。通过传递 0
的规模,iOS会自动为当前设备选择适当的值。
The code creates a new CoreGraphics
image context with the options passed as parameters; size, opaqueness, and scale. By passing 0
for scale, iOS automatically chooses the appropriate value for the current device.
color设置为 [UIColor whiteColor]
。立即,然后使用 UIRectFill()
并传递一个填充画布的矩形,实际上用该颜色填充画布。
Then, the context fill colour is set to [UIColor whiteColor]
. Immediately, the canvas is then actually filled with that color, by using UIRectFill()
and passing a rectangle which fills the canvas.
然后创建一个 UIImage
当前上下文,并关闭上下文。因此,图像变量包含所需大小的 UIImage
,填充为白色。
A UIImage
is then created of the current context, and the context is closed. Therefore, the image variable contains a UIImage
of the desired size, filled white.
这篇关于如何编程UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!