From d15d0bf221893df55333cbee96c2bd421f13fc8a Mon Sep 17 00:00:00 2001 From: Nora Trapp Date: Fri, 23 Jul 2021 16:21:53 -0700 Subject: [PATCH] Preserve image orientation when downsampling is not required --- .../attachments/SignalAttachment.swift | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/SignalMessaging/attachments/SignalAttachment.swift b/SignalMessaging/attachments/SignalAttachment.swift index 1c1d64a66c..a86e4c6354 100644 --- a/SignalMessaging/attachments/SignalAttachment.swift +++ b/SignalMessaging/attachments/SignalAttachment.swift @@ -866,6 +866,7 @@ public class SignalAttachment: NSObject { let maxSize = imageUploadQuality.maxEdgeSize let pixelSize = dataSource.imageMetadata.pixelSize + var imageProperties = [CFString: Any]() let cgImage: CGImage if pixelSize.width > maxSize || pixelSize.height > maxSize { @@ -879,6 +880,20 @@ public class SignalAttachment: NSObject { return .error(error: .couldNotParseImage) } + guard let originalImageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, [ + kCGImageSourceShouldCache: false + ] as CFDictionary) as? [CFString: Any] else { + return .error(error: .couldNotParseImage) + } + + // Preserve any orientation properties in the final output image. + if let tiffOrientation = originalImageProperties[kCGImagePropertyTIFFOrientation] { + imageProperties[kCGImagePropertyTIFFOrientation] = tiffOrientation + } + if let iptcOrientation = originalImageProperties[kCGImagePropertyIPTCImageOrientation] { + imageProperties[kCGImagePropertyIPTCImageOrientation] = iptcOrientation + } + guard let image = CGImageSourceCreateImageAtIndex(imageSource, 0, [ kCGImageSourceShouldCacheImmediately: true ] as CFDictionary) else { @@ -894,7 +909,6 @@ public class SignalAttachment: NSObject { let dataFileExtension: String let dataUTI: CFString let dataMIMEType: String - let imageProperties: CFDictionary? // We convert everything that's not sticker-like to jpg, because // often images with alpha channels don't actually have any @@ -905,12 +919,11 @@ public class SignalAttachment: NSObject { dataFileExtension = "png" dataUTI = kUTTypePNG dataMIMEType = OWSMimeTypeImagePng - imageProperties = nil } else { dataFileExtension = "jpg" dataUTI = kUTTypeJPEG dataMIMEType = OWSMimeTypeImageJpeg - imageProperties = [kCGImageDestinationLossyCompressionQuality: compressionQuality(for: pixelSize)] as CFDictionary + imageProperties[kCGImageDestinationLossyCompressionQuality] = compressionQuality(for: pixelSize) } let tempFileUrl = OWSFileSystem.temporaryFileUrl(fileExtension: dataFileExtension) @@ -918,7 +931,7 @@ public class SignalAttachment: NSObject { owsFailDebug("Failed to create CGImageDestination for attachment") return .error(error: .couldNotConvertImage) } - CGImageDestinationAddImage(destination, cgImage, imageProperties) + CGImageDestinationAddImage(destination, cgImage, imageProperties as CFDictionary) guard CGImageDestinationFinalize(destination) else { owsFailDebug("Failed to write downsampled attachment to disk") return .error(error: .couldNotConvertImage)