From b2956b6118449c5e529f0c5875f23d58dc6f4ab8 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 31 Dec 2019 16:41:56 -0500 Subject: [PATCH] Convert HEIC images to JPEG before upload Fixes #60 --- .../Screens/Compose/ComposeViewController.swift | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Tusker/Screens/Compose/ComposeViewController.swift b/Tusker/Screens/Compose/ComposeViewController.swift index 51751f09af..c72f4bf9ff 100644 --- a/Tusker/Screens/Compose/ComposeViewController.swift +++ b/Tusker/Screens/Compose/ComposeViewController.swift @@ -493,10 +493,20 @@ class ComposeViewController: UIViewController { options.resizeMode = .none options.isNetworkAccessAllowed = true PHImageManager.default().requestImageDataAndOrientation(for: asset, options: options) { (data, dataUTI, orientation, info) in - guard let data = data, let dataUTI = dataUTI else { fatalError() } - - let mimeType = UTTypeCopyPreferredTagWithClass(dataUTI as CFString, kUTTagClassMIMEType)!.takeRetainedValue() as String + guard var data = data, let dataUTI = dataUTI else { fatalError() } + let mimeType: String + if dataUTI == "public.heic" { + // neither Mastodon nor Pleroma handles HEIC well, so convert to JPEG + let image = CIImage(data: data)! + let context = CIContext() + let colorSpace = image.colorSpace ?? CGColorSpace(name: CGColorSpace.sRGB)! + data = context.jpegRepresentation(of: image, colorSpace: colorSpace, options: [:])! + mimeType = "image/jpeg" + } else { + mimeType = UTTypeCopyPreferredTagWithClass(dataUTI as CFString, kUTTagClassMIMEType)!.takeRetainedValue() as String + } + self.postProgressView.step() let request = MastodonController.client.upload(attachment: FormAttachment(mimeType: mimeType, data: data, fileName: "file"), description: description)