Created
September 10, 2020 08:14
-
-
Save yimajo/d814de1743377c383b1e1723642fd45d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import MobileCoreServices | |
import CoreImage | |
public struct JPEGConverter { | |
let data: Data | |
let dataUTI: String | |
public init(data: Data, with dataUTI: String) { | |
self.data = data | |
self.dataUTI = dataUTI | |
} | |
public func convertJPEG() -> Data? { | |
if UTTypeConformsTo(dataUTI as CFString, kUTTypeJPEG) { | |
return data | |
} else if dataUTI.range(of: ".heic") != nil || dataUTI.range(of: ".heif") != nil { | |
guard let ciImage = CIImage(data: data) else { | |
fatalError("Can't convert despite HEIF.") | |
} | |
return CIContext().jpegRepresentation( | |
of: ciImage, | |
colorSpace: CGColorSpaceCreateDeviceRGB() | |
)! | |
} else { | |
return nil | |
} | |
} | |
public func callAsFunction() -> Data? { | |
convertJPEG() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment