如何以编程方式制作UIImage? [英] How can I make an UIImage programmatically?

查看:65
本文介绍了如何以编程方式制作UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能不是您最初想的那样。我知道如何使用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。视图范围];

这些尺寸。无论如何,我想知道如何创建那些尺寸全部变为白色的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.

这甚至可能吗?我确定是的,但是也许我错了。

Is this even possible? I am sure it is, but maybe I am wrong.

这必须是白色 图片。不是一个空白。 :)

This needs to be a "white" image. Not a blank one. :)

推荐答案

您需要按以下方式使用 CoreGraphics

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.

然后,上下文填充颜色设置为 [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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆