title: CFDataRef and Pixel Data for iPhone Programming
body: <p>I am trying to access the raw pixels of an image, but I keep running into a problem that got me really stuck. I am pretty new to iPhone programming so it might be something really simple...</p>
  
  <p>Here is the class I have, it uses a <code>CGImageRef</code> from an image and looks at the individual pixels. Here is my code</p>
  
  <p>The header file:</p>
  
  <pre><code>#import &lt;QuartzCore/QuartzCore.h&gt;
  #import &lt;QuartzCore/QuartzCore.h&gt;
  #import &lt;CoreImage/CoreImage.h&gt;
  
  
  @interface GraphingView : UIControl 
  {
  @private
      CAShapeLayer *shapeLayer;    
      CGImageRef pixelData;
  }
  
  @property(assign) CGImageRef pixelData;
  @end
  </code></pre>
  
  <p>And my implementation file:</p>
  
  <pre><code>#import "GraphingView.h"
  #import "iSpectrumViewController.h"
  
  @implementation GraphingView
  
  @synthesize pixelData;
  - (void)awakeFromNib
  {
      // set up a rounded border
      CALayer *layer = [self layer];
  
      // clear the view's background color so that our background
      // fits within the rounded border
      CGColorRef backgroundColor = [self.backgroundColor CGColor];
      self.backgroundColor = [UIColor clearColor];
      layer.backgroundColor = backgroundColor;
  
  
      shapeLayer = [CAShapeLayer layer];      
  
      [self setNeedsDisplay];
  }
  
  - (void)drawRect:(CGRect)rect
  {
  
  }
  
  - (void)updateGraph
  {
      // This is where my problem is:
       // Whenever the next line is executed the application stops working. It works fine in the setPixelData
       // down below.
      CFDataRef pixelRef = CGDataProviderCopyData(CGImageGetDataProvider(pixelData));
      CFIndex byteSize = CFDataGetLength(pixelRef);
      int intByteSize = (int) byteSize;
      NSLog(@"Number of Bytes contained %i",intByteSize);
  
       //Then get a pointer to the data so the information can be accessed
      const UInt8 *pixelPtr = CFDataGetBytePtr(pixelRef);
  
  
      // Do some drawing here using the data obtained from the image...
  
      [shapeLayer setPath:path];
      [shapeLayer setNeedsDisplay];
      CFRelease(path);    
  }
  
  -(void)setPixelData:(CGImageRef)pixelImage
  {
       //This code takes the CGImage from my image and copies it to the pixelData CGImageRef defined in the header file
  
  
      //This gets the CFDataRef from the CGImage so the pixel information is available
      CFDataRef PixelCopy = CGDataProviderCopyData(CGImageGetDataProvider(pixelImage));
      CFIndex byteSize = CFDataGetLength(PixelCopy);
      int intByteSize = (int) byteSize;
      NSLog(@"Number of Bytes contained %i",intByteSize);
  
      pixelData = CGImageCreateCopy(pixelImage);
      [self updateGraph];
  
  }
  @end
  </code></pre>
  
  <p>The problem I have is:</p>
  
  <p>The function <code>setPixelData</code> gets the <code>CGImageRef</code> image and copies it to <code>pixelData</code>. Then, up in the <code>(void) updateGraph</code> method, I use the <code>CGImageRef pixelData</code> and create a <code>CFDataRef</code> to get the raw pixel data. However, there is something wrong. Because when I use the <code>CFDataGetLength</code> method using the <code>CFDataRef pixelRef</code> as input I get the <code>EXC_BAD_ACCESS</code> error when the program is run. What confuses me is that when I then put the same two lines of code in the <code>(void) setPixelData</code> function  (shown in the code above) and comment out the code in <code>updateGraph</code> then the program runs perfectly fine.</p>
  
  <p>Any ideas of what could be causing this error,  and how to eliminate it?</p>
  
  <p>Thanks,</p>
  
  <p>Sam</p>