title: UIImage Upside Down When Reconverted From String Stored in SQLite
body: <p>I save an image from the simulator photo library.</p>
  <p><a href="https://i.stack.imgur.com/gYibC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gYibC.png" alt="Original image from stock photo library" /></a></p>
  <p>I then save the image as a string and when the user clicks Save, it is reconverted from the string into an image and placed in a table view in another controller. For some reason, only certain stock photos are being reconverted upside down.</p>
  <p><a href="https://i.stack.imgur.com/Z1unF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z1unF.png" alt="Upside down image after reconverted back to image from string" /></a></p>
  <p><strong>Code for converting image to string:</strong></p>
  <pre><code>extension UIImage
  {
      func toString() -&gt; String?
      {
          let data: Data? = self.pngData()
          return data?.base64EncodedString(options: .lineLength64Characters)
      }
  }
  </code></pre>
  <p><strong>Code to reconvert string back to image:</strong></p>
  <pre><code>extension String
  {
      func toImage() -&gt; UIImage?
      {
          if let data = Data(base64Encoded: self, options: .ignoreUnknownCharacters)
          {
              return UIImage(data: data)
          }
      
          return nil
     }
  }
  </code></pre>
  <p>The conversion is a derivation based on another StackOverflow page: <a href="https://stackoverflow.com/questions/11251340/convert-between-uiimage-and-base64-string">Convert between UIImage and Base64 string</a></p>
  <p><strong>Database store code for entire 'family':</strong></p>
  <pre><code>    /**
  INSERT operation prepared statement for family
  
  - Parameters
      - family: Contains the data for each child and parents' name and image
  */
  func insertFamily(family: Family)
  {
      var insertStatement: OpaquePointer? = nil
      let result = sqlite3_prepare_v2(self.db, self.insertFamilyQuery, -1, &amp;insertStatement, nil)
      
      if result == SQLITE_OK
      {
          // Bind values to insert statement
          sqlite3_bind_text(insertStatement, 1, (family.childName! as NSString).utf8String, -1, nil)
          sqlite3_bind_text(insertStatement, 2, (family.childImage! as NSString).utf8String, -1, nil)
          sqlite3_bind_text(insertStatement, 3, (family.parentOneName! as NSString).utf8String, -1, nil)
          sqlite3_bind_text(insertStatement, 4, (family.parentOneImage! as NSString).utf8String, -1, nil)
          sqlite3_bind_text(insertStatement, 5, (family.parentTwoName! as NSString).utf8String, -1, nil)
          sqlite3_bind_text(insertStatement, 6, (family.parentTwoImage! as NSString).utf8String, -1, nil)
          
          // Execute statement
          let result = sqlite3_step(insertStatement)
          sqlite3_finalize(insertStatement)   // Destroy statement to avoid memory leak
          
          if result == SQLITE_DONE
          {
              os_log(&quot;Inserted family for %s&quot;, log: self.oslog, type: .info, family.childName!)
          }
          else
          {
              os_log(&quot;Could not insert family&quot;, log: self.oslog, type: .error)
              os_log(&quot;Expected: %s Received: %s&quot;, log: self.oslog, type: .error, SQLITE_DONE, result)
          }
      }
      else 
      {
          os_log(&quot;INSERT statement could not be prepared&quot;, log: self.oslog, type: .error)
          os_log(&quot;Expected: %s Received: %s&quot;, log: self.oslog, type: .error, SQLITE_DONE, result)
      }
  }
  </code></pre>
  <p><strong>Database read code for entire 'family':</strong></p>
  <pre><code>    /**
  Pull the family for the given childName if it exists
  
  - Parameters
      - childName: The child's name associated with the family
  
  - Returns: The family found
  */
  func pullFamily(childName: String) -&gt; Family?
  {
      var family = Family()
      var readStatement: OpaquePointer? = nil
      let selectStatement = self.pullFamilyQuery + childName + &quot;'&quot;
      
      // Read
      if sqlite3_prepare_v2(self.db, selectStatement, -1, &amp;readStatement, nil) == SQLITE_OK
      {
          if sqlite3_step(readStatement) == SQLITE_ROW
          {
              let childName = String(describing: String(cString: sqlite3_column_text(readStatement, 0)))
              let childImage = String(describing: String(cString: sqlite3_column_text(readStatement, 1)))
              let parentOneName = String(describing: String(cString: sqlite3_column_text(readStatement, 2)))
              let parentOneImage = String(describing: String(cString: sqlite3_column_text(readStatement, 3)))
              let parentTwoName = String(describing: String(cString: sqlite3_column_text(readStatement, 4)))
              let parentTwoImage = String(describing: String(cString: sqlite3_column_text(readStatement, 5)))
              
              family = Family(childName: childName, 
                                  childImage: childImage, 
                                  parentOneName: parentOneName, 
                                  parentOneImage: parentOneImage, 
                                  parentTwoName: parentTwoName, 
                                  parentTwoImage: parentTwoImage)
          }
      }
      else 
      {
          os_log(&quot;Could not pull family by child name &quot;, log: self.oslog, type: .error, childName)
      }
      
      sqlite3_finalize(readStatement) // Destroy statement to avoid memory leak
      return family
  }
  </code></pre>
  <p>Family Initializer</p>
  <pre><code>init(childName: String?, childImage: UIImage?,
       parentOneName: String?, parentOneImage: UIImage?,
       parentTwoName: String?, parentTwoImage: UIImage?)
      {
          self.childName = childName
          self.childImage = childImage
          self.parentOneName = parentOneName
          self.parentOneImage = parentOneImage
          self.parentTwoName = parentTwoName
          self.parentTwoImage = parentTwoImage
      }
  </code></pre>
  <p>Test Code for Blob Write</p>
  <pre><code>// Child Image
  result = (family.childImage!.unrotatedPngData()?.withUnsafeBytes { bufferPointer -&gt; Int32 in
          sqlite3_bind_blob(insertStatement, 1, bufferPointer.baseAddress, Int32(bufferPointer.count), SQLITE_TRANSIENT) })!
  
  guard result == SQLITE_OK else {
      logger.error(&quot;[\(#function); \(#line)] ERROR: Could not bind child image&quot;)
      logger.error(&quot;[\(#function); \(#line)] ERROR: Expected - \(SQLITE_OK), Received - \(result)&quot;)
      logger.error(&quot;[\(#function); \(#line)] [ERROR: \(self.getDatabaseError())&quot;)
      return
  </code></pre>
  <p>Test Code for Blob Read</p>
  <pre><code>while sqlite3_step(readStatement) == SQLITE_ROW
  {
      guard
          let childName = sqlite3_column_text(readStatement, 0).flatMap({ String(cString: $0) }),
          let childImageBlob = sqlite3_column_blob(readStatement, 1)
          //let parentOneName = sqlite3_column_text(readStatement, 2).flatMap({ String(cString: $0) }),
          //let parentOneImageBlob = sqlite3_column_blob(readStatement, 3),
          //let parentTwoName = sqlite3_column_text(readStatement, 4).flatMap({ String(cString: $0) }),
          //let parentTwoImageBlob = sqlite3_column_blob(readStatement, 5)
      else {
          logger.error(&quot;[\(#function); \(#line)] Could not pull family&quot;)
          logger.error(&quot;\(String(cString: sqlite3_errmsg(self.db)))&quot;)
          return families
      }
              
      // Convert child image data to child image
      let childData = Data(bytes: childImageBlob, count: Int(sqlite3_column_bytes(readStatement, 1)))
      guard let childImage = UIImage(data: childData) else {
          logger.error(&quot;Could not convert child image for child name \(childName)&quot;)
          return families
      }
  
      ...
  }
  </code></pre>