From b663335c6d17d38b89bdbf12fa35c79dd4196714 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 22 Aug 2024 13:54:03 -0400 Subject: [PATCH] Use the image description from imported image when possible Closes #523 --- .../ComposeUI/CoreData/DraftAttachment.swift | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/CoreData/DraftAttachment.swift b/Packages/ComposeUI/Sources/ComposeUI/CoreData/DraftAttachment.swift index 64ecf945..d71fb082 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/CoreData/DraftAttachment.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/CoreData/DraftAttachment.swift @@ -167,11 +167,23 @@ extension DraftAttachment: NSItemProviderReading { type = .png } + // Read the caption from the image itself, if there is one. + let caption: String + if let source = CGImageSourceCreateWithData(data as CFData, [kCGImageSourceTypeIdentifierHint: typeIdentifier as CFString] as CFDictionary), + let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [String: Any], + // This is the dictionary for TIFF properties, but it's present for other image types too + let tiffProperties = properties[kCGImagePropertyTIFFDictionary as String] as? [String: Any], + let imageDescription = tiffProperties[kCGImagePropertyTIFFImageDescription as String] as? String { + caption = imageDescription + } else { + caption = "" + } + let attachment = DraftAttachment(entity: DraftsPersistentContainer.shared.persistentStoreCoordinator.managedObjectModel.entitiesByName["DraftAttachment"]!, insertInto: nil) attachment.id = UUID() attachment.fileURL = try writeDataToFile(data, id: attachment.id, type: type) attachment.fileType = type.identifier - attachment.attachmentDescription = "" + attachment.attachmentDescription = caption return attachment }