Turn Pixels On

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

  1. macrumors member

    Joined:
    Jul 1, 2011
    #1
    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:
    Code:
    turnPixelOn:(UIView *)aView atCoordinates:(CGPoint)aPoint withColor:(UIColor*)aColor.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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. thread starter macrumors member

    Joined:
    Jul 1, 2011
    #3
    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. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  5. macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #5
    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. macrumors regular

    Joined:
    Jun 7, 2009
    #6
    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