title: iPhone UIImage loaded from core data is rotated counterclockwise by 90 degrees. How to fix?
body: <p>I'm capturing an image from camera using AVCaptureSession:</p>
  
  <pre><code>- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
  </code></pre>
  
  <p>The image gets converted into a UIImage and is added to an image view. The image is displayed in a portrait orientation, as expected.</p>
  
  <p>I'm trying to save the image into core data between capture sessions. My core data entity has an image property with binary data type. </p>
  
  <pre><code>coreDataEntity.image = UIImagePNGRepresentation(imageView.image);
  </code></pre>
  
  <p>Once the user reloads a previously saved session, the image view has its image restored using: </p>
  
  <pre><code> imageView.image = [UIImage imageWithData:coreDataEntity.image];
  </code></pre>
  
  <p>However, the restored image is rotated 90 degrees counterclockwise with it's top pointing to the left. </p>
  
  <p><strong>At which point does the image get rotated?</strong> Is it during saving or during loading from core data?</p>
  
  <p>I have a method to rotate a UIImage by 90 degrees, but it appears that on subsequent re-loading of the same image, it gets rotated by extra 90 degrees. How can I know that I've already rotated a UIImage once?</p>
  
  <p>Thank you for any help!</p>