@@ -19,66 +19,39 @@ class RunnerHolder: ObservableObject {
19
19
extension UIImage {
20
20
func resized( to newSize: CGSize ) -> UIImage {
21
21
let format = UIGraphicsImageRendererFormat . default ( )
22
- let renderer = UIGraphicsImageRenderer ( size : newSize , format : format )
23
- let image = renderer . image { _ in
24
- draw ( in: CGRect ( origin: . zero, size: newSize) )
22
+ format . scale = 1
23
+ return UIGraphicsImageRenderer ( size : newSize , format : format ) . image {
24
+ _ in draw ( in: CGRect ( origin: . zero, size: newSize) )
25
25
}
26
26
return image
27
27
}
28
28
29
29
func toRGBArray( ) -> [ UInt8 ] ? {
30
- guard let cgImage = self . cgImage else {
31
- NSLog ( " Failed to get CGImage from UIImage " )
32
- return nil
33
- }
30
+ guard let cgImage = self . cgImage else { return nil }
34
31
35
- let width = cgImage. width
36
- let height = cgImage. height
37
- let colorSpace = CGColorSpaceCreateDeviceRGB ( )
38
- let bytesPerPixel = 4
39
- let bytesPerRow = bytesPerPixel * width
40
- let bitsPerComponent = 8
41
- let bitmapInfo = CGImageAlphaInfo . premultipliedLast. rawValue
32
+ let width = Int ( cgImage. width) , height = Int ( cgImage. height)
33
+ let totalPixels = width * height, bytesPerPixel = 4 , bytesPerRow = bytesPerPixel * width
34
+ var rgbValues = [ UInt8] ( repeating: 0 , count: totalPixels * 3 )
35
+ var pixelData = [ UInt8] ( repeating: 0 , count: width * height * bytesPerPixel)
42
36
43
37
guard let context = CGContext (
44
- data: nil ,
45
- width: width,
46
- height: height,
47
- bitsPerComponent: bitsPerComponent,
48
- bytesPerRow: bytesPerRow,
49
- space: colorSpace,
50
- bitmapInfo: bitmapInfo
51
- ) else {
52
- NSLog ( " Failed to create CGContext " )
53
- return nil
54
- }
38
+ data: & pixelData, width: width, height: height, bitsPerComponent: 8 ,
39
+ bytesPerRow: bytesPerRow, space: CGColorSpaceCreateDeviceRGB ( ) ,
40
+ bitmapInfo: CGImageAlphaInfo . premultipliedLast. rawValue | CGBitmapInfo . byteOrder32Big. rawValue
41
+ ) else { return nil }
55
42
56
43
context. draw ( cgImage, in: CGRect ( x: 0 , y: 0 , width: width, height: height) )
57
44
58
- guard let pixelBuffer = context. data else {
59
- NSLog ( " Failed to get pixel data from CGContext " )
60
- return nil
61
- }
62
-
63
- let pixelData = pixelBuffer. bindMemory ( to: UInt8 . self, capacity: width * height * bytesPerPixel)
64
-
65
- var rgbArray = [ UInt8] ( repeating: 0 , count: width * height * 3 )
66
-
67
45
for y in 0 ..< height {
68
46
for x in 0 ..< width {
69
47
let pixelIndex = ( y * width + x) * bytesPerPixel
70
- let r = UInt8 ( pixelData [ pixelIndex] )
71
- let g = UInt8 ( pixelData [ pixelIndex + 1 ] )
72
- let b = UInt8 ( pixelData [ pixelIndex + 2 ] )
73
-
74
- let rgbIndex = ( y * width + x)
75
- rgbArray [ rgbIndex] = r
76
- rgbArray [ rgbIndex + height * width] = g
77
- rgbArray [ rgbIndex + 2 * height * width] = b
48
+ let rgbIndex = y * width + x
49
+ rgbValues [ rgbIndex] = pixelData [ pixelIndex]
50
+ rgbValues [ rgbIndex + totalPixels] = pixelData [ pixelIndex + 1 ]
51
+ rgbValues [ rgbIndex + totalPixels * 2 ] = pixelData [ pixelIndex + 2 ]
78
52
}
79
53
}
80
-
81
- return rgbArray
54
+ return rgbValues
82
55
}
83
56
}
84
57
0 commit comments