title: Storing images in CloudKit as CKAsset, image upside-down
body: <p>I am working on developing an app that uses CloudKit to store and retrieve image files as <code>CKAsset</code> objects.  Normally, this works great and I love CloudKit's small learning curve. Periodically though, I have this peculiar issue where an image is being stored either totally upside-down, or 90º to the left or to the right. Here is an example of an image that was stored improperly:</p>
  
  <p><img src="https://i.stack.imgur.com/ouOz1.png" alt="Example of my MacBook curiously rotated 90º to the left"></p>
  
  <p>I took that picture with my iPhone 5s, in portrait orientation (I didn't save the original image to my device, as I only intended to save it in the cloud or else I'd post the original here too.  I figured since it's a MacBook Pro sitting at a table you could figure out the original intended orientation of the picture). Anyways, I have this problem not only with images fresh from the camera, but also with an image chosen from my photo library.  </p>
  
  <p>Here is the code I use to store the image via CloudKit:</p>
  
  <pre><code>let newRecord:CKRecord = CKRecord(recordType: "ImageProject")
  let imageData:NSData = UIImagePNGRepresentation(game.referenceImage)!
  let path = self.documentsDirectoryPath.stringByAppendingPathComponent(self.imageDirectoryName)
  imageURL = NSURL(fileURLWithPath: path)
  imageData.writeToURL(imageURL, atomically: true)
  UIImagePNGRepresentation(game.referenceImage)!.writeToFile(path, atomically: true)
  let imageFile:CKAsset?  = CKAsset(fileURL: NSURL(fileURLWithPath: path))
  
  newRecord.setValue(imageFile, forKey: "referenceImage")
  
      // Save the record into the public database
      if let database = self.publicDatabase {
  
        database.saveRecord(newRecord, completionHandler: { (record:CKRecord?, error:NSError?) in
  
              // Check if there was an error
              if error != nil {
                  NSLog((error?.localizedDescription)!)
              }
              else {
                  // There wasn't an error
                  dispatch_async(dispatch_get_main_queue()) {
  
                          if let savedRecord = record {
                              print("Image Successfully Saved")
                          }
                  }
              }
          })
        }
  </code></pre>
  
  <p>Here the <code>game</code> is just an <code>NSObject</code> I have created called <code>SavedGame</code>, that just stores variables, specifically for this case the <code>referenceImage</code> as a <code>UIImage</code>. I'm pretty sure that my retrieval process is not the issue, because I noticed when I go to the CK Dashboard and download the image after it's been stored, it's rotated there as well.  Just in case, here is the code I use to retrieve the image:</p>
  
  <pre><code>// Initialize the database and start the query
  ..........
  for record in returnedRecords {
      if let retrievedImage = record.objectForKey("referenceImage") as! CKAsset? {
           let newGame:SavedGame = SavedGame()                  
           if let data = NSData(contentsOfURL: retrievedImage.fileURL) {
                newGame.referenceImage =  UIImage(data: data)!
           }
       }
  }
  </code></pre>
  
  <p>Could anybody give me some sort of guidance as to where I might be making a mistake? This problem is not consistent at all, it's very strange.  I would estimate it happens maybe 5-10% of the time.  I don't know if its a CK issue, or a problem with my code. Any help would be greatly appreciated.  Thanks in advance!</p>