Fix converting to color space.
This commit is contained in:
parent
a89abc06db
commit
3658e8b19c
|
@ -1059,7 +1059,7 @@
|
|||
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
|
||||
CODE_SIGN_IDENTITY = "Apple Development";
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 41;
|
||||
CURRENT_PROJECT_VERSION = 42;
|
||||
DEVELOPMENT_ASSET_PATHS = "\"Vernissage/Preview Content\"";
|
||||
DEVELOPMENT_TEAM = B2U9FEKYP8;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
|
@ -1096,7 +1096,7 @@
|
|||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
||||
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 41;
|
||||
CURRENT_PROJECT_VERSION = 42;
|
||||
DEVELOPMENT_ASSET_PATHS = "\"Vernissage/Preview Content\"";
|
||||
DEVELOPMENT_TEAM = B2U9FEKYP8;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
|
|
|
@ -6,10 +6,83 @@
|
|||
|
||||
import UIKit
|
||||
|
||||
extension UIImage {
|
||||
func resized(to size: CGSize) -> UIImage {
|
||||
UIGraphicsImageRenderer(size: size).image { _ in
|
||||
draw(in: CGRect(origin: .zero, size: size))
|
||||
extension UIImage.Orientation {
|
||||
var exifOrientation: Int32 {
|
||||
switch self {
|
||||
case .up: return 1
|
||||
case .down: return 3
|
||||
case .left: return 8
|
||||
case .right: return 6
|
||||
case .upMirrored: return 2
|
||||
case .downMirrored: return 4
|
||||
case .leftMirrored: return 5
|
||||
case .rightMirrored: return 7
|
||||
@unknown default: return 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension UIImage {
|
||||
func resized(to targetSize: CGSize) -> UIImage {
|
||||
guard let sourceImage = CIImage(image: self, options: [.applyOrientationProperty: true]) else {
|
||||
return self
|
||||
}
|
||||
|
||||
// We have to store correct image orientation.
|
||||
let orientedImage = sourceImage.oriented(forExifOrientation: self.imageOrientation.exifOrientation)
|
||||
|
||||
// Filter.
|
||||
let resizeFilter = CIFilter(name:"CILanczosScaleTransform")!
|
||||
|
||||
// Compute scale.
|
||||
let scale = targetSize.width / orientedImage.extent.width
|
||||
|
||||
// Apply resizing
|
||||
resizeFilter.setValue(orientedImage, forKey: kCIInputImageKey)
|
||||
resizeFilter.setValue(scale, forKey: kCIInputScaleKey)
|
||||
resizeFilter.setValue(1.0, forKey: kCIInputAspectRatioKey)
|
||||
|
||||
guard let result = resizeFilter.value(forKey: kCIOutputImageKey) as? CIImage else {
|
||||
return self
|
||||
}
|
||||
|
||||
guard let resizedCGImage = CIContext(options: nil).createCGImage(result, from: result.extent) else {
|
||||
return self
|
||||
}
|
||||
|
||||
return UIImage(cgImage: resizedCGImage)
|
||||
}
|
||||
|
||||
func convertToExtendedSRGBJpeg() -> Data? {
|
||||
guard let sourceImage = CIImage(image: self, options: [.applyOrientationProperty: true]) else {
|
||||
return self.jpegData(compressionQuality: 0.9)
|
||||
}
|
||||
|
||||
// We have to store correct image orientation.
|
||||
let orientedImage = sourceImage.oriented(forExifOrientation: self.imageOrientation.exifOrientation)
|
||||
|
||||
// We dont have to convert images which already are in sRGB color space.
|
||||
if orientedImage.colorSpace?.name == CGColorSpace.sRGB || orientedImage.colorSpace?.name == CGColorSpace.extendedSRGB {
|
||||
return self.jpegData(compressionQuality: 0.9)
|
||||
}
|
||||
|
||||
guard let colorSpace = CGColorSpace(name: CGColorSpace.extendedSRGB) else {
|
||||
return self.jpegData(compressionQuality: 0.9)
|
||||
}
|
||||
|
||||
guard let displayP3 = CGColorSpace(name: CGColorSpace.displayP3) else {
|
||||
return self.jpegData(compressionQuality: 0.9)
|
||||
}
|
||||
|
||||
// Create Core Image context (with working color space).
|
||||
let ciContext = CIContext(options: [CIContextOption.workingColorSpace: orientedImage.colorSpace ?? displayP3])
|
||||
|
||||
// Creating image with new color space (and preserving colors).
|
||||
guard let converted = ciContext.jpegRepresentation(of: orientedImage, colorSpace: colorSpace) else {
|
||||
return self.jpegData(compressionQuality: 0.9)
|
||||
}
|
||||
|
||||
// Returning successfully converted image.
|
||||
return converted
|
||||
}
|
||||
}
|
||||
|
|
|
@ -572,18 +572,26 @@ struct ComposeView: View {
|
|||
}
|
||||
|
||||
private func getJpegData(image: UIImage) -> Data? {
|
||||
#if targetEnvironment(simulator)
|
||||
// For testing purposes.
|
||||
let converted = image.convertToExtendedSRGBJpeg()
|
||||
let filePath = URL.temporaryDirectory.appending(path: "\(UUID().uuidString).jpg")
|
||||
try? converted?.write(to: filePath)
|
||||
print(filePath.string)
|
||||
#endif
|
||||
|
||||
// API don't support images over 5K.
|
||||
if image.size.height > 10_000 || image.size.width > 10_000 {
|
||||
return image
|
||||
.resized(to: .init(width: image.size.width / 4, height: image.size.height / 4))
|
||||
.jpegData(compressionQuality: 0.90)
|
||||
.convertToExtendedSRGBJpeg()
|
||||
} else if image.size.height > 5000 || image.size.width > 5000 {
|
||||
return image
|
||||
.resized(to: .init(width: image.size.width / 2, height: image.size.height / 2))
|
||||
.jpegData(compressionQuality: 0.90)
|
||||
.convertToExtendedSRGBJpeg()
|
||||
} else {
|
||||
return image
|
||||
.jpegData(compressionQuality: 0.90)
|
||||
.convertToExtendedSRGBJpeg()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue