title: Photo Editing Extension - Revert Image to Original
body: <p>I'm developing a photo editing extension for iOS, bundled in a container app that provides the same photo editing functionality.</p>
  
  <p>In order to reuse code, I have a view controller class that adopts the required <code>PHContentEditingController</code> protocol, and I subclassed it for use both as the main interface of the app extension, and as the "working screen" of the container app.</p>
  
  <hr>
  
  <p><strong><em>On the editing extension,</em></strong> the controller's methods are called by the Photos app's editing session as described in Apple's documentation and various tutorials you can find around the web.</p>
  
  <p><strong><em>On the container app,</em></strong> on the other hand, I first obtain a <code>PHAsset</code> instance by means of the <code>UIImagePickerController</code> class, and directly start the editing session manually on my "work" view controller like this:</p>
  
  <pre><code>// 'work' is my view controller which adopts
  // `PHContentEditingController`. 'workNavigation'
  // embeds it.
  
  let options = PHContentEditingInputRequestOptions()
  options.canHandleAdjustmentData = { (adjustmentData) in
      return work.canHandle(adjustmentData)
  }
  
  asset.requestContentEditingInput(with: options, completionHandler: { [weak self] (input, options) in
      // (Called on the Main thread on iOS 10.0 and above)
      guard let this = self else {
          return
      }
      guard let editingInput = input else {
          return
      }
      work.asset = asset
      work.startContentEditing(with: editingInput, placeholderImage: editingInput.displaySizeImage!)
      this.present(workNavigation, animated: true, completion: nil)
  })
  </code></pre>
  
  <hr>
  
  <p><strong>When the user finishes editing</strong>, the work view controller calls <code>finishContentEditing(completionHandler:</code> on itself to finish the session:</p>
  
  <pre><code>self.finishContentEditing(completionHandler: {(output) in
      // nil output results in "Revert" prompt.
      // non-nil output results in "Modify" prompt. 
  
      let library = PHPhotoLibrary.shared()
      library.performChanges({
          let request = PHAssetChangeRequest(for: self.asset)
          request.contentEditingOutput = output
  
      }, completionHandler: {(didSave, error) in
          if let error = error {
              // handle error...
  
          } else if didSave {
              // proceed after saving...
  
          } else {
              // proceed after cancellation...
          }
      })
  })
  </code></pre>
  
  <p>Within the editing session, the user can 'clear' the previous edits passed as <strong>adjustment data</strong>, effectively reverting the image to its original state. 
  I've noticed that, if I finish the editing by calling the completion handler passed to <code>finishContentEditing(completionHandler:)</code> with <code>nil</code> as its argument (instead of a valid <code>PHContentEditingOutput</code> object), the Photos framework will prompt the user to "revert" the image instead of "modifying" it:</p>
  
  <pre><code>func finishContentEditing(completionHandler: @escaping (PHContentEditingOutput?) -&gt; Void) {
  
      guard let editingInput = self.editingInput, let inputURL = editingInput.fullSizeImageURL else {
          return completionHandler(nil)
      }
  
      if editingInput.adjustmentData != nil &amp;&amp; hasUnsavedEdits == false {
          // We began with non-nil adjustment data but now have 
          // no outstanding edits - means REVERT:
  
          return completionHandler(nil)
      }
  
      // (...proceed with writing output to url...)
  </code></pre>
  
  <p><strong>However, this only works when running from the container app.</strong> If I try the same trick from the <em>extension</em> (i.e., load an image that contains previous edits, reset them, and tap 'Done') I get the dreaded <strong>"Unable to Save Changes"</strong> message... </p>
  
  <hr>
  
  <p><strong>What is the correct way to revert previous edits to an image from within a photo editing extension?</strong></p>