0

I'm new to Swift, and my project is reading every pixel of an image, and then returns the RGB value for each pixel. I'm able to load the Image from URL. How can I return the Pixel RGB similar to below:

pix 0 rgb 0 118 195
pix 1 rgb 0 119 196
pix 2 rgb 0 118 194

Thank you!

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let imageUrlString = "https://sff-koi.com/wp/wp-content/uploads/2021/06/AJKS-2000-32nd-champion-600x840.jpg"
        guard let imageUrl:URL = URL(string: imageUrlString) else {
            return
        }
        
        // Start background thread so that image loading does not make app unresponsive
          DispatchQueue.global().async { [weak self] in
            
            guard let self = self else { return }
            
            guard let imageData = try? Data(contentsOf: imageUrl) else {
                return
            }
   
            let imageView = UIImageView(frame: CGRect(x:0, y:0, width:400, height:400))
            imageView.center = self.view.center
            
            // When from a background thread, UI needs to be updated on main_queue
           DispatchQueue.main.async {
                let image = UIImage(data: imageData)
                imageView.image = image
                imageView.contentMode = UIView.ContentMode.scaleAspectFit
                self.view.addSubview(imageView)
            }
        }
 
        self.view = view
    }
}

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

`

I tried to follow this thread, but I couldn't figure out how to link my photo to the RGB function

timbre timbre
  • 12,648
  • 10
  • 46
  • 77
Huy Ly
  • 1
  • Sometimes, people write “SWIFT”. What is the cultural reason for this? –  Nov 27 '22 at 07:47
  • 1
    As an iOS developer working at a large bank, I believe it has to do with the SWIFT banking standards lol https://www.swift.com/ – Brianna Doubt Nov 27 '22 at 08:30
  • You said you tried that linked code. Show what you tried and clearly explain what help you need to get it to work. – HangarRash Nov 27 '22 at 16:34
  • Does this answer your question? [Swift 4: Get RGB values of pixel in UIImage](https://stackoverflow.com/questions/50623967/swift-4-get-rgb-values-of-pixel-in-uiimage) – timbre timbre Nov 27 '22 at 21:21

0 Answers0