View Full Version : Turn Pixels On

Jul 19, 2011, 01:43 PM
I'm trying to make an app in which the user enter an equation (i.e y=2x) and the iPhone will graph it on screen. I am wondering how to turn on individual pixels on the iPhone screen so I can draw the graph. At first I tried creating a whole bunch of rectangles with height and width of 1, but the program ran way too slowly trying to allocate and initialize all those rectangles. So I'm wondering if there's a simple command for turning on pixels, something like: turnPixelOn:(UIView *)aView atCoordinates:(CGPoint)aPoint withColor:(UIColor*)aColor.

Jul 19, 2011, 01:46 PM
You cannot "turn a pixel on". You can easily create a bitmap image, get the raw data array that represents that image and with a bit of knowledge about the structure of the image (you can get this with low level CoreGraphics functions) you can manipulate the pixels directly.

Note this is a reasonably complex thing to do and I'm certainly not about to post code.

Jul 19, 2011, 01:48 PM
OK, thanks. I read about the bitmapping and I was just trying to find an easier way to graph an equation. But if this is what it takes....

Jul 19, 2011, 01:58 PM
You could try adding a UIWebView and using a Google Chart (http://code.google.com/apis/chart/).

Jul 19, 2011, 02:06 PM
You can subclass UIView and implement your own drawRect method to make your graph or you can draw directly into a bitmap and present that bitmap as a view.

Either way you need to use CoreGraphics to perform the actual drawing. Check out the WWDC 2011 videos, session 129 "Practical Drawing for iOS Developers" shows how to draw a graph.

Jul 19, 2011, 11:28 PM
The first thing that I would try for graphing is drawing lines with a CGPathRef. I think that would be pretty fast and easy to do, probably less code than you have now with the 1x1 rectangles.