Turn Pixels On

Discussion in 'iOS Programming' started by xcodeNewbie, Jul 19, 2011.

  1. xcodeNewbie macrumors member

    Jul 1, 2011
    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.
  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    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.
  3. xcodeNewbie thread starter macrumors member

    Jul 1, 2011
    Drawing Pixels

    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....
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
  5. bredell macrumors regular

    Mar 30, 2008
    Uppsala, Sweden
    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.
  6. rossipoo macrumors regular

    Jun 7, 2009
    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.

Share This Page