Preserve image orientation when downsampling is not required
This commit is contained in:
parent
214bc7f303
commit
d15d0bf221
@ -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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user