From 2b2d042085e3fd49b7645a1239c43b28dfd498f3 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Sat, 11 Feb 2023 20:39:56 +0100 Subject: [PATCH] Fix pasting image from clipboard on iOS + reduce JPEG further --- .../StatusEditorUTTypeSupported.swift | 17 ++++++++++++++++- .../Status/Editor/StatusEditorViewModel.swift | 7 +++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Packages/Status/Sources/Status/Editor/Components/StatusEditorUTTypeSupported.swift b/Packages/Status/Sources/Status/Editor/Components/StatusEditorUTTypeSupported.swift index cf5798ed..15e5ec86 100644 --- a/Packages/Status/Sources/Status/Editor/Components/StatusEditorUTTypeSupported.swift +++ b/Packages/Status/Sources/Status/Editor/Components/StatusEditorUTTypeSupported.swift @@ -59,6 +59,8 @@ enum StatusEditorUTTypeSupported: String, CaseIterable { } else if let data = result as? Data, let image = UIImage(data: data) { return image + } else if let transferable = await getImageTansferable(item: item){ + return transferable } } if let url = result as? URL { @@ -97,6 +99,19 @@ enum StatusEditorUTTypeSupported: String, CaseIterable { } } } + + private func getImageTansferable(item: NSItemProvider) async -> ImageFileTranseferable? { + return await withCheckedContinuation { continuation in + _ = item.loadTransferable(type: ImageFileTranseferable.self) { result in + switch result { + case let .success(success): + continuation.resume(with: .success(success)) + case .failure: + continuation.resume(with: .success(nil)) + } + } + } + } } struct MovieFileTranseferable: Transferable { @@ -133,7 +148,7 @@ struct ImageFileTranseferable: Transferable { let url: URL lazy var data: Data? = try? Data(contentsOf: url) - lazy var compressedData: Data? = image?.jpegData(compressionQuality: 0.90) + lazy var compressedData: Data? = image?.jpegData(compressionQuality: 0.80) lazy var image: UIImage? = UIImage(data: data ?? Data()) static var transferRepresentation: some TransferRepresentation { diff --git a/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift b/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift index ad87563f..2d492e31 100644 --- a/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift +++ b/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift @@ -370,6 +370,13 @@ public class StatusEditorViewModel: NSObject, ObservableObject { gifTransferable: nil, mediaAttachment: nil, error: nil)) + } else if var content = content as? ImageFileTranseferable, + let image = content.image { + mediasImages.append(.init(image: image, + movieTransferable: nil, + gifTransferable: nil, + mediaAttachment: nil, + error: nil)) } else if let video = content as? MovieFileTranseferable { mediasImages.append(.init(image: nil, movieTransferable: video,