title: rotate CGImage on disk using CGImageSource/CGImageDestination?
body: <p>I'm working on an application that needs to take a picture using <code>UIImagePickerController</code>, display a thumbnail of it in the app, and also submit that picture to a server using <code>ASIFormDataRequest</code> (from <code>ASIHTTPRequest</code>).</p>
  
  <p>I want to use <code>setFile:withFileName:andContentType:forKey:</code> from ASIFormDataRequest since in my experience it's faster than trying to submit an image using <code>UIImageJPEGRepresentation</code> and submitting raw NSData. To that end I'm using <a href="http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGImageDestination/Reference/reference.html" rel="nofollow"><code>CGImageDestination</code></a> and creating an image destination with a url, saving that image to disk, and then uploading that file on disk.</p>
  
  <p>In order to create the thumbnail I'm using <code>CGImageSourceCreateThumbnailAtIndex</code> (see <a href="http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGImageSource/Reference/reference.html" rel="nofollow">docs</a>) and creating an image source with the path of the file I just saved.</p>
  
  <p>My problem is that no matter what options I pass into the image destination or the thumbnail creation call, my thumbnail always comes out rotated 90 degrees counterclockwise. The uploaded image is also rotated. I've tried explicitly setting the orientation in the options of the image using <code>CGImageDestinationSetProperties</code> but it doesn't seem to take. The only solution I've found is to rotate the image in memory, but I really want to avoid that since doing so doubles the time it takes for the thumbnail+saving operation to finish. Ideally I'd like to be able to rotate the image on disk.</p>
  
  <p>Am I missing something in how I'm using <code>CGImageDestination</code> or <code>CGImageSource</code>? I'll post some code below.</p>
  
  <p><strong>Saving the image:</strong></p>
  
  <pre><code>NSURL *filePath = [NSURL fileURLWithPath:self.imagePath];
  CGImageRef imageRef = [self.image CGImage];
  CGImageDestinationRef ref = CGImageDestinationCreateWithURL((CFURLRef)filePath, kUTTypeJPEG, 1, NULL);
  CGImageDestinationAddImage(ref, imageRef, NULL);
  
  NSDictionary *props = [[NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat:1.0], kCGImageDestinationLossyCompressionQuality,
                          nil] retain];
  //Note that setting kCGImagePropertyOrientation didn't work here for me
  
  CGImageDestinationSetProperties(ref, (CFDictionaryRef) props);
  
  CGImageDestinationFinalize(ref);
  CFRelease(ref);
  </code></pre>
  
  <p><strong>Generating the thumbnail</strong></p>
  
  <pre><code>CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)filePath, NULL);
  if (!imageSource)
      return;
  
  CFDictionaryRef options = (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
                                              (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
                                              (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
                                              (id)[NSNumber numberWithFloat:THUMBNAIL_SIDE_LENGTH], (id)kCGImageSourceThumbnailMaxPixelSize, nil];
  CGImageRef imgRef = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options);
  UIImage *thumb = [UIImage imageWithCGImage:imgRef];
  
  CGImageRelease(imgRef);
  CFRelease(imageSource);
  </code></pre>
  
  <p>And then to upload the image I just use</p>
  
  <pre><code>[request setFile:path withFileName:fileName andContentType:contentType forKey:@"photo"];
  </code></pre>
  
  <p>where <code>path</code> is the path to the file saved with the code above.</p>