From 34e57c297b5bd8b06188cc0455bedcaf331ab3f8 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 3 Nov 2023 11:07:43 -0400 Subject: [PATCH] Tweak HEIF/HEIC handling --- .../ComposeUI/CoreData/DraftAttachment.swift | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/CoreData/DraftAttachment.swift b/Packages/ComposeUI/Sources/ComposeUI/CoreData/DraftAttachment.swift index 16f2443d..0def2175 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/CoreData/DraftAttachment.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/CoreData/DraftAttachment.swift @@ -137,6 +137,8 @@ extension DraftAttachment { //private let attachmentTypeIdentifier = "space.vaccor.Tusker.composition-attachment" private let imageType = UTType.image.identifier +private let heifType = UTType.heif.identifier +private let heicType = UTType.heic.identifier private let jpegType = UTType.jpeg.identifier private let pngType = UTType.png.identifier private let mp4Type = UTType.mpeg4Movie.identifier @@ -148,7 +150,7 @@ 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, imageType, mp4Type, quickTimeType] + [/*typeIdentifier, */ gifType, heifType, heicType, jpegType, pngType, imageType, mp4Type, quickTimeType] } public static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> DraftAttachment { @@ -273,20 +275,13 @@ extension DraftAttachment { var data = data var type = type - if type != .png && type != .jpeg, - let image = UIImage(data: data) { - // The quality of 0.8 was chosen completely arbitrarily, it may need to be tuned in the future. - data = image.jpegData(compressionQuality: 0.8)! - type = .jpeg - } - let image = CIImage(data: data)! let needsColorSpaceConversion = features.needsWideColorGamutHack && image.colorSpace?.name != CGColorSpace.sRGB // neither Mastodon nor Pleroma handles HEIC well, so convert to JPEG // they also do a bad job converting wide color gamut images (they seem to just drop the profile, letting the wide-gamut values be reinterprete as sRGB) // if that changes in the future, we'll need to pass the InstanceFeatures in here somehow and gate the conversion - if needsColorSpaceConversion || type == .heic { + if needsColorSpaceConversion || type == .heic || type == .heif { let context = CIContext() let colorSpace = needsColorSpaceConversion || image.colorSpace != nil ? CGColorSpace(name: CGColorSpace.sRGB)! : image.colorSpace! if type == .png {