@@ -25,15 +25,28 @@ private let imageCompressionQuality: CGFloat = 0.8
25
25
/// For some image types like `CIImage`, creating valid model content requires creating a JPEG
26
26
/// representation of the image that may not yet exist, which may be computationally expensive.
27
27
public enum ImageConversionError : Error {
28
+ /// The image that could not be converted.
29
+ public enum SourceImage {
30
+ #if canImport(UIKit)
31
+ case uiImage( UIImage )
32
+ #elseif canImport(AppKit)
33
+ case nsImage( NSImage )
34
+ #endif // canImport(UIKit)
35
+ case cgImage( CGImage )
36
+ #if canImport(CoreImage)
37
+ case ciImage( CIImage )
38
+ #endif // canImport(CoreImage)
39
+ }
40
+
28
41
/// The image (the receiver of the call `toModelContentParts()`) was invalid.
29
42
case invalidUnderlyingImage
30
43
31
44
/// A valid image destination could not be allocated.
32
45
case couldNotAllocateDestination
33
46
34
47
/// JPEG image data conversion failed, accompanied by the original image, which may be an
35
- /// instance of `NSImageRep `, `UIImage`, `CGImage`, or `CIImage`.
36
- case couldNotConvertToJPEG( Any )
48
+ /// instance of `NSImage `, `UIImage`, `CGImage`, or `CIImage`.
49
+ case couldNotConvertToJPEG( SourceImage )
37
50
}
38
51
39
52
#if canImport(UIKit)
@@ -42,7 +55,7 @@ public enum ImageConversionError: Error {
42
55
extension UIImage : ThrowingPartsRepresentable {
43
56
public func tryPartsValue( ) throws -> [ ModelContent . Part ] {
44
57
guard let data = jpegData ( compressionQuality: imageCompressionQuality) else {
45
- throw ImageConversionError . couldNotConvertToJPEG ( self )
58
+ throw ImageConversionError . couldNotConvertToJPEG ( . uiImage ( self ) )
46
59
}
47
60
return [ ModelContent . Part. data ( mimetype: " image/jpeg " , data) ]
48
61
}
@@ -59,7 +72,7 @@ public enum ImageConversionError: Error {
59
72
let bmp = NSBitmapImageRep ( cgImage: cgImage)
60
73
guard let data = bmp. representation ( using: . jpeg, properties: [ . compressionFactor: 0.8 ] )
61
74
else {
62
- throw ImageConversionError . couldNotConvertToJPEG ( bmp )
75
+ throw ImageConversionError . couldNotConvertToJPEG ( . nsImage ( self ) )
63
76
}
64
77
return [ ModelContent . Part. data ( mimetype: " image/jpeg " , data) ]
65
78
}
@@ -84,7 +97,7 @@ public enum ImageConversionError: Error {
84
97
if CGImageDestinationFinalize ( imageDestination) {
85
98
return [ . data( mimetype: " image/jpeg " , output as Data ) ]
86
99
}
87
- throw ImageConversionError . couldNotConvertToJPEG ( self )
100
+ throw ImageConversionError . couldNotConvertToJPEG ( . cgImage ( self ) )
88
101
}
89
102
}
90
103
#endif // !os(watchOS)
@@ -105,7 +118,7 @@ public enum ImageConversionError: Error {
105
118
if let jpegData = jpegData {
106
119
return [ . data( mimetype: " image/jpeg " , jpegData) ]
107
120
}
108
- throw ImageConversionError . couldNotConvertToJPEG ( self )
121
+ throw ImageConversionError . couldNotConvertToJPEG ( . ciImage ( self ) )
109
122
}
110
123
}
111
124
#endif // canImport(CoreImage)
0 commit comments