From 68d02fb77d1e0cd4942b99b53a514f79f5348180 Mon Sep 17 00:00:00 2001 From: Luca Topolovec Date: Thu, 6 Jul 2023 17:30:15 +0200 Subject: [PATCH] CATTY-717 Allow selection of images from files Implementation of feature CATTY-717 Allow selection of images from files --- ...ooksTableViewController+MediaLibrary.swift | 50 +++++++++++++++++++ .../MaintainLooks/LooksTableViewController.m | 10 +++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/Catty/ViewController/Continue&New/MaintainObject/MaintainLooks/LooksTableViewController+MediaLibrary.swift b/src/Catty/ViewController/Continue&New/MaintainObject/MaintainLooks/LooksTableViewController+MediaLibrary.swift index c693e93e72..b2afde0aa1 100644 --- a/src/Catty/ViewController/Continue&New/MaintainObject/MaintainLooks/LooksTableViewController+MediaLibrary.swift +++ b/src/Catty/ViewController/Continue&New/MaintainObject/MaintainLooks/LooksTableViewController+MediaLibrary.swift @@ -20,6 +20,8 @@ * along with this program. If not, see http://www.gnu.org/licenses/. */ +import MobileCoreServices + extension LooksTableViewController { @objc @@ -60,3 +62,51 @@ extension LooksTableViewController: MediaLibraryViewControllerImportDelegate { } } } + +extension LooksTableViewController { + @available(iOS 14.0, *) + static var supportedFileFormats = [UTType.png, UTType.jpeg] + + @objc + func showImagesSelectFile() { + var documentPicker: UIDocumentPickerViewController + if #available(iOS 14.0, *) { + documentPicker = UIDocumentPickerViewController.init(forOpeningContentTypes: type(of: self).supportedFileFormats, asCopy: true) + documentPicker.allowsMultipleSelection = false + documentPicker.delegate = self + documentPicker.modalPresentationStyle = .formSheet + present(documentPicker, animated: true) + } + } +} + +extension LooksTableViewController: UIDocumentPickerDelegate, UINavigationControllerDelegate { + public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { + let documents = URL(fileURLWithPath: + CBFileManager.shared().documentsDirectory) + + for url in urls { + let fileName = UUID().uuidString + let name = url.deletingPathExtension().lastPathComponent + let fileURL = documents + .appendingPathComponent(fileName) + .appendingPathExtension(url.pathExtension) + + do { + let data = try Data.init(contentsOf: url) + try data.write(to: fileURL, options: .atomic) + if let image = UIImage(data: data) { + self.addMediaLibraryLoadedImage(image, withName: name) + continue + } + } catch { + self.showImportAlert(itemName: name) + } + } + controller.dismiss(animated: true) + } + + public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { + controller.dismiss(animated: true) + } +} diff --git a/src/Catty/ViewController/Continue&New/MaintainObject/MaintainLooks/LooksTableViewController.m b/src/Catty/ViewController/Continue&New/MaintainObject/MaintainLooks/LooksTableViewController.m index 7d6e67b3fd..667b8b3058 100644 --- a/src/Catty/ViewController/Continue&New/MaintainObject/MaintainLooks/LooksTableViewController.m +++ b/src/Catty/ViewController/Continue&New/MaintainObject/MaintainLooks/LooksTableViewController.m @@ -637,7 +637,7 @@ - (void)showAddLookActionSheet } } - [[[[actionSheet + [[[[[actionSheet addDefaultActionWithTitle:kLocalizedDrawNewImage handler:^{ dispatch_async(dispatch_get_main_queue(), ^{ PaintViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:kPaintViewControllerIdentifier]; @@ -658,7 +658,13 @@ - (void)showAddLookActionSheet [self showLooksMediaLibrary]; } }); - }] build] + }] + addDefaultActionWithTitle:kLocalizedSelectFile handler:^{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self showImagesSelectFile]; + }); + }] + build] showWithController:self]; }