1

我正在尝试为我在 Objective-c 中编写的类创建随机生成的图像,并且只是希望对不同的图像属性和类型有所了解。我想创建以下功能:

-(UIImage*)randomlyGenerateAnImage;

该函数将使用成员数据创建一个准随机算法生成的 UIImage。

我真的很难理解 UIImages、CGRects 和 CGContextRefs 之间的关系。哪个是我绘制的,我分配给 UIImage 什么。

编辑 我正在尝试动态生成我的图像,例如:

UIImage * newImage = [[UIImage alloc] "initBlankImageWithSize(X, Y)"];

CGContextRef * newContext = "getContextFromImage(newImage)";

"在 newContext 上绘制......";

返回新图像;

4

2 回答 2

0

You need a context where you can draw in. You can get the current context with UIGraphicsGetCurrentContext().

You can create an image from the current context via UIGraphicsGetImageFromCurrentImageContext()

The CGRect is a rectangle inside your view.

You should read Apple's drawing guide to get more informations.

于 2012-07-11T16:35:54.533 回答
0

对于UIViewController由 a 填充的 aUIImageView你会做

UIGraphicsBeginImageContext(self.view.frame.size);
[sigBox.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//drawing code that says CGContext a lot
sigBox.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

借鉴UIImage“拥有”的UIImageView。因此,您绘制CGContext由 a 定义的CGRect,然后您可以UIImage从中返回 a CGContext。我怀疑这种解释是否能让任何事情变得更清楚,但希望代码能做到。

于 2012-07-11T16:33:33.693 回答