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