@@ -25,15 +25,28 @@ private let imageCompressionQuality: CGFloat = 0.8
2525/// For some image types like `CIImage`, creating valid model content requires creating a JPEG
2626/// representation of the image that may not yet exist, which may be computationally expensive.
2727public 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+
2841 /// The image (the receiver of the call `toModelContentParts()`) was invalid.
2942 case invalidUnderlyingImage
3043
3144 /// A valid image destination could not be allocated.
3245 case couldNotAllocateDestination
3346
3447 /// 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 )
3750}
3851
3952#if canImport(UIKit)
@@ -42,7 +55,7 @@ public enum ImageConversionError: Error {
4255 extension UIImage : ThrowingPartsRepresentable {
4356 public func tryPartsValue( ) throws -> [ ModelContent . Part ] {
4457 guard let data = jpegData ( compressionQuality: imageCompressionQuality) else {
45- throw ImageConversionError . couldNotConvertToJPEG ( self )
58+ throw ImageConversionError . couldNotConvertToJPEG ( . uiImage ( self ) )
4659 }
4760 return [ ModelContent . Part. data ( mimetype: " image/jpeg " , data) ]
4861 }
@@ -59,7 +72,7 @@ public enum ImageConversionError: Error {
5972 let bmp = NSBitmapImageRep ( cgImage: cgImage)
6073 guard let data = bmp. representation ( using: . jpeg, properties: [ . compressionFactor: 0.8 ] )
6174 else {
62- throw ImageConversionError . couldNotConvertToJPEG ( bmp )
75+ throw ImageConversionError . couldNotConvertToJPEG ( . nsImage ( self ) )
6376 }
6477 return [ ModelContent . Part. data ( mimetype: " image/jpeg " , data) ]
6578 }
@@ -84,7 +97,7 @@ public enum ImageConversionError: Error {
8497 if CGImageDestinationFinalize ( imageDestination) {
8598 return [ . data( mimetype: " image/jpeg " , output as Data ) ]
8699 }
87- throw ImageConversionError . couldNotConvertToJPEG ( self )
100+ throw ImageConversionError . couldNotConvertToJPEG ( . cgImage ( self ) )
88101 }
89102 }
90103#endif // !os(watchOS)
@@ -105,7 +118,7 @@ public enum ImageConversionError: Error {
105118 if let jpegData = jpegData {
106119 return [ . data( mimetype: " image/jpeg " , jpegData) ]
107120 }
108- throw ImageConversionError . couldNotConvertToJPEG ( self )
121+ throw ImageConversionError . couldNotConvertToJPEG ( . ciImage ( self ) )
109122 }
110123 }
111124#endif // canImport(CoreImage)
0 commit comments