Skip to content

Commit

Permalink
CATTY-717 Allow selection of images from files
Browse files Browse the repository at this point in the history
Implementation of feature CATTY-717 Allow selection of images from files
  • Loading branch information
lucatp committed Jul 6, 2023
1 parent e488257 commit 68d02fb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

import MobileCoreServices

extension LooksTableViewController {

@objc
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ - (void)showAddLookActionSheet
}
}

[[[[actionSheet
[[[[[actionSheet
addDefaultActionWithTitle:kLocalizedDrawNewImage handler:^{
dispatch_async(dispatch_get_main_queue(), ^{
PaintViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:kPaintViewControllerIdentifier];
Expand All @@ -658,7 +658,13 @@ - (void)showAddLookActionSheet
[self showLooksMediaLibrary];
}
});
}] build]
}]
addDefaultActionWithTitle:kLocalizedSelectFile handler:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self showImagesSelectFile];
});
}]
build]
showWithController:self];
}

Expand Down

0 comments on commit 68d02fb

Please sign in to comment.