ρσݥzση
ρσݥzση

Reputation: 217

Objective-C iOS How to draw images to screen

I am going into creating games for iOS devices and I want to be able to draw images to the screen with my own image renderer class.

I am looking for a method similar to the one used in microsoft XNA where you draw images each frame and I am wondering how to go aout doing that.

I have had experience in objective-c before and I am very experienced in C# (I do understand this is a different ball game) but I was half expecting CoreGraphics to be like a Grapics Object in c#. However I cannot wrap my head around drawing to a screen. The only method I can find is by overriding he drawRect method but that is useless to me as I will be drawing different things at different times and need it to be dynamic.

I have thought about drawing each image using core graphics and i have also thought about another method involving drawing a 1 px rectangle for each pixel in each image im drawing but I do not know how well that would work.

So I need help with: - Drawing images (or rectangles) from a method in a seperate image renderer class.

Thanks in advance for any help, and I appolagize about the long post.

Upvotes: 1

Views: 1597

Answers (2)

AliSoftware
AliSoftware

Reputation: 32681

1) For all the "synching" part, namely draw a new frame each time the screen is refreshed, you should use the CADisplayLink class that allows your to specify a method of your choice to be called each time a frame needs to be drawn.

The called method will have the CADisplayLink object passed as its parameter, to which you can query the timestamp and so that you know which frame you need to draw. For example if you have a game with a ball moving, you can compare the timestamp of the current frame with the timestamp of the last drawn frame and know how much time elapsed, so as you probably know you ball's velocity and last position, you can easily determine its new position where you need to draw it.

2) For the rest, namely all the drawing part, except if you choose to use OpenGL (in this case take a look at GLKit to make your code easier) you can use Core Graphics to do low-level drawing by yourself. For this you will find everything you need in the Quartz 2D Programming Guide, including how to draw pixels bezier path / vector graphics, and how to draw offscreen on a bitmap context.

Upvotes: 1

Pritam Barhate
Pritam Barhate

Reputation: 775

Drawing images (or rectangles) from a method in a seperate image renderer class.

This can be done using UIGraphicsBeginImageContext and UIGraphicsEndImageContext code block. The technique is basically, you begin the image context, use your drawing code, get an UIImage reference from the image context and end the image context.

For example the answer to question "How do I use the NSString draw functionality to create a UIImage from text" shows how you can create an image from a string.

Upvotes: 2

Related Questions