0

I want to use QLPreviewController to edit images in my app. Just using vanilla code to present this:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    collectionView.deselectItem(at: indexPath, animated: true)
    let quickLookViewController = QLPreviewController()
    quickLookViewController.dataSource = self
    quickLookViewController.delegate = self
    tappedCell = collectionView.cellForItem(at: indexPath) as? FileCell
    quickLookViewController.currentPreviewItemIndex = indexPath.row
    present(quickLookViewController, animated: true)
}

The result is as follows:

enter image description here

Is there a way to add cancel button functionality to the QLPreviewController? I've heard of putting it in a navigation controller and then adding buttons to the bar, but I don't want to do this since it's hacky and causes strange transition animation issues.

It seems strange there isn't a cancel button by default since if you annotate a photo form within the iOS photos app it provides both a "Done" and a "Cancel" button.

Kex
  • 8,023
  • 9
  • 56
  • 129
  • embed it in a navigationcontroller add a cancel button on that bar – Zeeshan Ahmad II Jun 08 '22 at 09:57
  • This is a hacky workaround. Note also that if you present a `QLPreviewController` that's embedded in a navigation controller the buttons will change as it presents. eg the right bar button will initially be a share button and then turn into the annotate button. – Kex Jun 08 '22 at 10:45
  • @Kex You can subclass `QLPreviewController` and `override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)`. You can present an alert controller and ask if the user would like to save or discard changes. – Leo Dabus Jun 09 '22 at 01:14
  • @LeoDabus does't `QLPreviewController` always save by default when dismissed though? – Kex Jun 09 '22 at 03:55
  • No you can implement your own `didSaveEditedCopyOf` method. It is been a while when I did it but I had the option to discard changes, save a copy or overwrite the file. Make sure to use editingModeFor `createCopy` – Leo Dabus Jun 09 '22 at 04:00

0 Answers0