Browse Source

added crop in QuickLook

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 years ago
parent
commit
70e0142c3f

+ 1 - 1
iOSClient/Main/Create cloud/NCUploadAssets.swift

@@ -246,7 +246,7 @@ struct UploadAssetsView: View {
             List {
 
                 if !uploadAssets.previewStore.isEmpty {
-                    Section(header: Text(NSLocalizedString("_crop_", comment: "")), footer: Text(NSLocalizedString("_modify_crop_desc_", comment: ""))) {
+                    Section(header: Text(NSLocalizedString("_crop_photo_", comment: "")), footer: Text(NSLocalizedString("_modify_crop_desc_", comment: ""))) {
                         ScrollView(.horizontal) {
                             LazyHGrid(rows: gridItems, alignment: .center, spacing: 10) {
                                 ForEach(0..<uploadAssets.previewStore.count, id: \.self) { index in

+ 2 - 1
iOSClient/Supporting Files/en.lproj/Localizable.strings

@@ -917,7 +917,8 @@
 "_mobile_config_"           = "Download the configuration profile";
 "_calendar_contacts_footer_"    = "After the downloading you need to go to the Settings app to install it. If a profile is not installed within 8 minutes of downloading it, it is automatically deleted";
 "_preview_"                 = "Preview";
-"_crop_"                    = "Crop photo";
+"_crop_photo_"              = "Crop photo";
+"_crop_"                    = "Crop";
 "_modify_crop_desc_"        = "Tap the image for cropping";
 
 // Video

+ 39 - 0
iOSClient/Viewer/NCViewerQuickLook/NCViewerQuickLook.swift

@@ -26,6 +26,7 @@
 import UIKit
 import QuickLook
 import NextcloudKit
+import Mantis
 
 @objc class NCViewerQuickLook: QLPreviewController {
 
@@ -70,6 +71,8 @@ import NextcloudKit
             let error = NKError(errorCode: NCGlobal.shared.errorCharactersForbidden, errorDescription: "_message_disable_overwrite_livephoto_")
             NCContentPresenter.shared.showInfo(error: error)
         }
+
+        navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_crop_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(crop))
     }
 
     override func viewDidAppear(_ animated: Bool) {
@@ -102,6 +105,23 @@ import NextcloudKit
         alertController.addAction(UIAlertAction(title: NSLocalizedString("_discard_changes_", comment: ""), style: .destructive) { _ in })
         parentVC?.present(alertController, animated: true)
     }
+
+    @objc func crop() {
+
+        guard let fileName = self.metadata?.fileNameView, let image = UIImage(contentsOfFile: NSTemporaryDirectory() + fileName) else { return }
+        let config = Mantis.Config()
+
+        if let bundleIdentifier = Bundle.main.bundleIdentifier {
+            config.localizationConfig.bundle = Bundle(identifier: bundleIdentifier)
+            config.localizationConfig.tableName = "Localizable"
+        }
+        let cropViewController = Mantis.cropViewController(image: image, config: config)
+
+        cropViewController.delegate = self
+        cropViewController.modalPresentationStyle = .fullScreen
+
+        self.present(cropViewController, animated: true)
+    }
 }
 
 extension NCViewerQuickLook: QLPreviewControllerDataSource, QLPreviewControllerDelegate {
@@ -165,6 +185,25 @@ extension NCViewerQuickLook: QLPreviewControllerDataSource, QLPreviewControllerD
     }
 }
 
+extension NCViewerQuickLook: CropViewControllerDelegate {
+
+    func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Mantis.Transformation, cropInfo: Mantis.CropInfo) {
+        cropViewController.dismiss(animated: true)
+
+        guard let data = cropped.jpegData(compressionQuality: 1) else { return }
+
+        do {
+            try data.write(to: self.url)
+            reloadData()
+        } catch {  }
+    }
+
+    func cropViewControllerDidCancel(_ cropViewController: Mantis.CropViewController, original: UIImage) {
+
+        cropViewController.dismiss(animated: true)
+    }
+}
+
 class PreviewItem: NSObject, QLPreviewItem {
     var previewItemURL: URL?
 }