-1

So I'm trying to make a program where Im taking a photo and it tells the user what the color is. I'm having trouble implementing the portion of func getPixelColor. Could anyone give me some tips on how to fuse this program together?

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var cameraButton: UIButton!
@IBOutlet weak var cameraView: UIImageView!
@IBOutlet weak var photosButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

@IBAction func tappedCameraButton(_ sender: Any) {
    let picker = UIImagePickerController()
    picker.sourceType = .camera
    picker.allowsEditing = true
    picker.delegate = self
    present(picker, animated: true)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
    cameraView?.image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
    picker.dismiss(animated: true, completion: nil)
}
extension UIImage {
    func getPixelColor(pos: CGPoint) -> UIColor {

        let pixelData = self.cgImage!.dataProvider!.data
        let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

        let pixelInfo: Int = ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4

        let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
        let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
        let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
        let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)

        return UIColor(red: r, green: g, blue: b, alpha: a)
    }

}

}

}

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 2
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Adrian Mole Jan 13 '21 at 23:51

2 Answers2

1

You need to get the CGPoint of the user touch and then use getPixelColor(pos:) to get the color.

There are two ways to get CGPoint

  1. You can use any of the methods to get the location touchesBegan(_:with:), touchesMoved, touchesEnded

  2. Use tap gesture

Here is the StackOverlow code the get the CGPoint: How to get a CGPoint from a tapped location?

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
0
extension UIImage {
     func getPixelColor(pos: CGPoint) -> UIColor? {
        
        guard let cgImg = cgImage, let provider = cgImg.dataProvider else {
            return nil
        }
        let pixelData = provider.data
        
        let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

        let pixelInfo: Int = ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4

        let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
        let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
        let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
        let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)
        return UIColor(red: r, green: g, blue: b, alpha: a)
     }
 }
dengST30
  • 3,643
  • 24
  • 25