title: Can I edit the pixels of the UIImage's property CGImage
body: <p>UIImage has a read-only property CGImage. I have to read its pixels to a memory block and edit  them and then make a new UIImage to replace the old one. I want to know if there is a way bypass the read-only property and edit those pixels directly.</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>Thanks all. I have found a way to do it. Write a class with those method:</p>
  
  <pre><code>-(void)preProcess:(UIImage*)srcImage {
      m_Context = ...// Created by calling CGBitmapContextCreate(...)
      ...
      CGContextDrawImage(m_Context, rect, srcImage.CGImage);
      m_Bits = (unsigned char*)CGBitmapContextGetData (mContext);
  }
  
  -(void)postProcess {
      CGContextRelease(m_Context);
      free(m_Bits);
  }
  
  -(UIImage*)doProcess:(CGPoint)pt {// just a example 
      unsigned char* ppxl = m_Bits + ...
      // do something...
      CGImageRef imRef = CGBitmapContextCreateImage(mContext);
      return [UIImage imageWithCGImage:imRef];
  }
  </code></pre>
  
  <p>And preProcess and postProcess are called just once.</p>