简体   繁体   中英

How do I get the color of a pixel in a UIImage?

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)
    }

}

}

}

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?

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)
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM