@@ -27,36 +27,43 @@ extension CGImage {
27
27
let outputSize = CGSize ( width: expectedWidth, height: expectedHeight)
28
28
let bitmapBytesPerRow = 0
29
29
30
- var context = CGContext ( data: nil ,
30
+ func getBitmapInfo( ) -> UInt32 {
31
+ if colorSpaceRef. model == . rgb {
32
+ switch ( bitsPerPixel, bitsPerComponent) {
33
+ case ( 16 , 5 ) :
34
+ return CGImageAlphaInfo . noneSkipFirst. rawValue
35
+ case ( 32 , 8 ) :
36
+ return CGImageAlphaInfo . premultipliedLast. rawValue
37
+ case ( 32 , 10 ) :
38
+ if #available( iOS 12 , macOS 10 . 14 , * ) {
39
+ return CGImageAlphaInfo . alphaOnly. rawValue | CGImagePixelFormatInfo . RGBCIF10. rawValue
40
+ } else {
41
+ return bitmapInfo. rawValue
42
+ }
43
+ case ( 64 , 16 ) :
44
+ return CGImageAlphaInfo . premultipliedLast. rawValue
45
+ case ( 128 , 32 ) :
46
+ return CGImageAlphaInfo . premultipliedLast. rawValue | CGBitmapInfo . floatComponents. rawValue
47
+ default :
48
+ return bitmapInfo. rawValue
49
+ }
50
+ }
51
+
52
+ return bitmapInfo. rawValue
53
+ }
54
+
55
+ guard let context = CGContext ( data: nil ,
31
56
width: Int ( outputSize. width) ,
32
57
height: Int ( outputSize. height) ,
33
- bitsPerComponent: self . bitsPerComponent,
58
+ bitsPerComponent: bitsPerComponent,
34
59
bytesPerRow: bitmapBytesPerRow,
35
60
space: colorSpaceRef,
36
- bitmapInfo: self . bitmapInfo. rawValue)
37
-
38
- if context == nil {
39
- context = CGContext ( data: nil ,
40
- width: Int ( outputSize. width) ,
41
- height: Int ( outputSize. height) ,
42
- bitsPerComponent: self . bitsPerComponent,
43
- bytesPerRow: bitmapBytesPerRow,
44
- space: colorSpaceRef,
45
- bitmapInfo: CGImageAlphaInfo . premultipliedLast. rawValue)
46
- }
47
-
48
- if context == nil {
49
- context = CGContext ( data: nil ,
50
- width: Int ( outputSize. width) ,
51
- height: Int ( outputSize. height) ,
52
- bitsPerComponent: self . bitsPerComponent,
53
- bytesPerRow: bitmapBytesPerRow,
54
- space: colorSpaceRef,
55
- bitmapInfo: CGImageAlphaInfo . premultipliedFirst. rawValue)
61
+ bitmapInfo: getBitmapInfo ( ) ) else {
62
+ return self
56
63
}
57
-
58
- context? . setFillColor ( UIColor . clear. cgColor)
59
- context? . fill ( CGRect ( x: 0 ,
64
+
65
+ context. setFillColor ( UIColor . clear. cgColor)
66
+ context. fill ( CGRect ( x: 0 ,
60
67
y: 0 ,
61
68
width: outputSize. width,
62
69
height: outputSize. height) )
@@ -66,15 +73,15 @@ extension CGImage {
66
73
uiCoords = uiCoords. translatedBy ( x: cropSize. width / 2 , y: cropSize. height / 2 )
67
74
uiCoords = uiCoords. scaledBy ( x: 1.0 , y: - 1.0 )
68
75
69
- context? . concatenate ( uiCoords)
70
- context? . concatenate ( transform)
71
- context? . scaleBy ( x: 1.0 , y: - 1.0 )
72
- context? . draw ( self , in: CGRect ( x: ( - imageViewSize . width / 2 ) ,
76
+ context. concatenate ( uiCoords)
77
+ context. concatenate ( transform)
78
+ context. scaleBy ( x: 1.0 , y: - 1.0 )
79
+ context. draw ( self , in: CGRect ( x: ( - imageViewSize . width / 2 ) ,
73
80
y: ( - imageViewSize . height / 2 ) ,
74
81
width: imageViewSize. width,
75
82
height: imageViewSize. height) )
76
83
77
- let result = context? . makeImage ( )
84
+ let result = context. makeImage ( )
78
85
79
86
return result
80
87
}
0 commit comments