Skip to content

Commit

Permalink
Allow built-in image editor to be presented when running iOS 14 or la…
Browse files Browse the repository at this point in the history
…ter.
  • Loading branch information
rnine committed Oct 4, 2021
1 parent 7ceccd5 commit 35a5026
Showing 1 changed file with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private extension ImagePickerUploadController {
}

@available(iOS 14.0, *)
func upload(results: [PHPickerResult]) {
func extract(results: [PHPickerResult], updateTrackingProgress: Bool = true, completion: @escaping (([URL]) -> Void)) {
let itemProviders = results.map(\.itemProvider)
let group = DispatchGroup()
var urls: [URL] = []
Expand All @@ -157,7 +157,9 @@ private extension ImagePickerUploadController {

progress.localizedDescription = "Fetching \(progress.totalUnitCount) photo album asset(s)…"

trackingProgress.update(tracked: progress)
if updateTrackingProgress {
trackingProgress.update(tracked: progress)
}

DispatchQueue.global(qos: .userInitiated).async {
for itemProvider in itemProviders {
Expand Down Expand Up @@ -201,8 +203,18 @@ private extension ImagePickerUploadController {
}

group.wait()
completion(urls)
}
}

self.upload(urls: urls)
@available(iOS 14.0, *)
func upload(results: [PHPickerResult]) {
extract(results: results) { urls in
if urls.count > 0 {
self.upload(urls: urls)
} else {
self.cancel()
}
}
}

Expand Down Expand Up @@ -240,8 +252,8 @@ private extension ImagePickerUploadController {
}

func editor(using image: UIImage) -> UIViewController {
return EditorViewController(image: image, completion: { image in
guard let image = image, let url = self.urlExtractor.fetchURL(image: image) else {
return EditorViewController(image: image, completion: { editedImage in
guard let image = editedImage, let url = self.urlExtractor.fetchURL(image: image) else {
self.cancel()
return
}
Expand All @@ -250,22 +262,40 @@ private extension ImagePickerUploadController {
})
}

func showEditor(with asset: PHAsset, on navigationController: UINavigationController) {
func showEditor(with asset: PHAsset) {
// Fetch uploadable completion
let completion: (Uploadable?, PHImageRequestID?) -> Void = { uploadable, requestID in
guard let image = uploadable as? UIImage else {
self.extractAndUpload(assets: [asset])
return
}

self.viewController.present(self.editor(using: image), animated: true)
self.showEditor(with: image)
}

guard uploadableExtractor.fetchUploadable(using: asset, completion: completion) != nil else {
extractAndUpload(assets: [asset])
return
}
}

@available(iOS 14.0, *)
func showEditor(with result: PHPickerResult) {
extract(results: [result], updateTrackingProgress: false) { urls in
guard let url = urls.first, let data = try? Data(contentsOf: url), let image = UIImage(data: data) else {
self.cancel()
return
}

DispatchQueue.main.async {
self.showEditor(with: image)
}
}
}

func showEditor(with image: UIImage) {
viewController.present(editor(using: image), animated: true)
}
}

// MARK: - PhotoPickerControllerDelegate Conformance
Expand All @@ -280,7 +310,7 @@ extension ImagePickerUploadController: PhotoPickerControllerDelegate {
func photoPicker(controller: UINavigationController, didSelectAssets assets: [PHAsset]) {
controller.dismiss(animated: true) {
if self.config.showEditorBeforeUpload, assets.count == 1, let asset = assets.first {
self.showEditor(with: asset, on: controller)
self.showEditor(with: asset)
} else {
self.extractAndUpload(assets: assets)
}
Expand Down Expand Up @@ -319,7 +349,11 @@ extension ImagePickerUploadController: UIImagePickerControllerDelegate & UINavig
extension ImagePickerUploadController: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true) {
self.upload(results: results)
if self.config.showEditorBeforeUpload, results.count == 1, let result = results.first {
self.showEditor(with: result)
} else {
self.upload(results: results)
}
}
}
}
Expand Down

0 comments on commit 35a5026

Please sign in to comment.