title: Converting NSData to CGImage and then back to NSData makes the file too big
body: <p>I have built a camera using <code>AVFoundation</code>. </p>
  
  <p>Once my <code>AVCaptureStillImageOutput</code> has completed its <code>captureStillImageAsynchronouslyFromConnection:completionHandler:</code> method, I create a NSData object like this: </p>
  
  <pre><code>         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
  </code></pre>
  
  <p>Once I have the <code>NSData</code> object, I would like to rotate the image -without- converting to a <code>UIImage</code>. I have found out that I can convert to a <code>CGImage</code> to do so.</p>
  
  <p>After I have the imageData, I start the process of converting to CGImage, but I have found that the <code>CGImageRef</code> ends up being THIRTY times larger than the <code>NSData</code> object.</p>
  
  <p>Here is the code I use to convert to <code>CGImage</code> from <code>NSData</code>: </p>
  
  <pre><code>CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)(imageData));
  CGImageRef imageRef = CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
  </code></pre>
  
  <p>If I try to <code>NSLog</code> out the size of the image, it comes to 30 megabytes when the <code>NSData</code> was a 1.5-2 megabyte image! </p>
  
  <pre><code>size_t imageSize = CGImageGetBytesPerRow(imageRef) * CGImageGetHeight(imageRef);
  
      NSLog(@"cgimage size = %zu",imageSize);
  </code></pre>
  
  <p>I thought that maybe when you go from NSData to CGImage, the image decompresses, and then maybe if I converted back to NSData, that it might go back to the right file size. </p>
  
  <pre><code>imageData = (NSData *) CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(imageRef)));
  </code></pre>
  
  <p>The above <code>NSData</code> has the same <code>length</code> as the <code>CGImageRef</code> object. </p>
  
  <p>If I try to save the image, the image is a 30mb image that cannot be opened. </p>
  
  <p>I am totally new to using CGImage, so I am not sure if I am converting from NSData to CGImage and back incorrectly, or if I need to call some method to decompress again.</p>
  
  <p>Thanks in advance,</p>
  
  <p>Will</p>