From 385f31728d47ca0f8e98b78a076750d6d59482dc Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 4 Jul 2023 11:07:35 -0700 Subject: [PATCH] Fix sharing screenshot from markup not working Closes #419 --- .../ComposeUI/CoreData/DraftAttachment.swift | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/CoreData/DraftAttachment.swift b/Packages/ComposeUI/Sources/ComposeUI/CoreData/DraftAttachment.swift index e3dfc428..463792d8 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/CoreData/DraftAttachment.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/CoreData/DraftAttachment.swift @@ -136,6 +136,7 @@ extension DraftAttachment { //private let attachmentTypeIdentifier = "space.vaccor.Tusker.composition-attachment" +private let imageType = UTType.image.identifier private let jpegType = UTType.jpeg.identifier private let pngType = UTType.png.identifier private let mp4Type = UTType.mpeg4Movie.identifier @@ -147,14 +148,26 @@ extension DraftAttachment: NSItemProviderReading { // todo: is there a better way of handling movies than manually adding all possible UTI types? // just using kUTTypeMovie doesn't work, because we need the actually type in order to get the file extension // without the file extension, getting the thumbnail and exporting the video for attachment upload fails - [/*typeIdentifier, */gifType, jpegType, pngType, mp4Type, quickTimeType] + [/*typeIdentifier, */ gifType, jpegType, pngType, imageType, mp4Type, quickTimeType] } public static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> DraftAttachment { + var data = data + var type = UTType(typeIdentifier)! + + // this seems to only occur when the item is a UIImage, rather than just image data, + // which seems to only occur when sharing a screenshot directly from the markup screen + if type == .image, + let image = try? NSKeyedUnarchiver.unarchivedObject(ofClass: UIImage.self, from: data), + let pngData = image.pngData() { + data = pngData + type = .png + } + let attachment = DraftAttachment(entity: DraftsPersistentContainer.shared.persistentStoreCoordinator.managedObjectModel.entitiesByName["DraftAttachment"]!, insertInto: nil) attachment.id = UUID() - attachment.fileURL = try writeDataToFile(data, id: attachment.id, type: UTType(typeIdentifier)!) - attachment.fileType = typeIdentifier + attachment.fileURL = try writeDataToFile(data, id: attachment.id, type: type) + attachment.fileType = type.identifier attachment.attachmentDescription = "" return attachment }