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