@@ -18,64 +18,39 @@ class RunnerHolder: ObservableObject {
1818
1919extension UIImage {
2020 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) )
2325 }
2426 }
2527
2628 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 }
3130
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)
3935
4036 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 }
5241
5342 context. draw ( cgImage, in: CGRect ( x: 0 , y: 0 , width: width, height: height) )
5443
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-
6444 for y in 0 ..< height {
6545 for x in 0 ..< width {
6646 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 ]
7551 }
7652 }
77-
78- return rgbArray
53+ return rgbValues
7954 }
8055}
8156
0 commit comments