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 }