1

I'm trying to create randomly generated images for a class I'm writing in objective-c, and was just hoping to get a little clarification as to different image properties and types. I want to create the following function:

-(UIImage*)randomlyGenerateAnImage;

This function will use member data to create a quasi-random algorithmically generated UIImage.

I'm really having trouble understanding the relation between UIImages, CGRects, and CGContextRefs. Which is the one I draw on and what do I assign to the UIImage.

Edit I'm trying to generate my image on the fly, something like:

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

CGContextRef * newContext = "getContextFromImage(newImage)";

"draw on newContext......";

return newImage;

BJ Myers
  • 6,617
  • 6
  • 34
  • 50
user1444872
  • 63
  • 1
  • 4
  • Not what you asked, but for 'creative coding' type practice you might find openFrameworks (http://www.openframeworks.cc/) easier to manage since a lot of the graphic pipeline plumbing is simplified – spring Jul 11 '12 at 18:32

2 Answers2

0

For a UIViewController that is filled up by a UIImageView you would do

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();

To draw on the UIImage "owned" by the UIImageView. So you draw on the CGContext that is defined by a CGRect, and then you can return a UIImage from the CGContext. I doubt that explanation made anything clearer, but hopefully the code did.

Dustin
  • 6,783
  • 4
  • 36
  • 53
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.

florianbuerger
  • 422
  • 2
  • 9