title: Given a CIImage, what is the fastest way to write image data to disk?
body: <p>I'm working with PhotoKit and have implemented filters users can apply to photos in their Photo Library. I am currently obtaining the image, applying a filter, returning the edited version as a <code>CIImage</code>, then I convert the <code>CIImage</code> into <code>NSData</code> using <code>UIImageJPEGRepresentation</code> so I may write that out to disk. While this works beautifully, when users attempt to edit really large (like 30 MB) photos it can take upwards of 30 seconds for this to occur, with 98% of the time spent on <code>UIImageJPEGRepresentation</code> (stat obtained from Instruments). </p>
  
  <p>I am looking for a more efficient way to save this edited photo to disk without compromising quality, if possible.</p>
  
  <p>I understand <code>UIImagePNGRepresentation</code> may result in improved quality, but this is even slower than the JPEG representation. </p>
  
  <p>This is what I am currently doing, on the default priority thread (<code>qos_class_utility</code>):</p>
  
  <pre><code>func jpegRepresentationOfImage(image: CIImage) -&gt; NSData {
      let eaglContext = EAGLContext(API: .OpenGLES2)
      let ciContext = CIContext(EAGLContext: eaglContext)
  
      let outputImageRef = ciContext.createCGImage(image, fromRect: image.extent())
      let uiImage = UIImage(CGImage: outputImageRef, scale: 1.0, orientation: UIImageOrientation.Up)
  
      return UIImageJPEGRepresentation(uiImage, 0.9) //this takes upwards of 20-30 seconds with large photos!
  }
  
  //writing out to disk:
  var error: NSError?
  let success = jpegData.writeToURL(contentEditingOutput.renderedContentURL, options: NSDataWritingOptions.AtomicWrite, error: &amp;error)
  </code></pre>