iOS – Obtaining the colour value of a specified pixel in a UIImage

To obtain the color value of a specified pixel in a UIImage, we can use the following code snippet in Swift:

extension UIImage {
    func getPixelColor(pos: CGPoint) -> UIColor? {
        guard let pixelData = self.cgImage?.dataProvider?.data else {
        return nil
        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)

This code snippet is an extension of UIImage that adds a method called getPixelColor that takes a CGPoint as an argument and returns a UIColor object that represents the colour of the specified pixel.

Here’s how it works:

  1. We’re using a guard let statement to conditionally unwrap pixelData and return nil if pixelData is not available.
  2. It then gets a pointer to the pixel data using CFDataGetBytePtr(pixelData).
  3. It calculates the index of the specified pixel using ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4.
    • The reason why we multiply pixelInfo by 4 is because each pixel in an image has four components (red, green, blue, and alpha), and each component is represented by one byte (8 bits). Therefore, each pixel takes up 4 bytes of memory.
    • In the getPixelColor function, we’re using pixelInfo to calculate the index of the pixel data in the image data buffer. Since each pixel takes up 4 bytes of memory, we need to multiply pixelInfo by 4 to get the correct index.
  4. It then extracts the red, green, blue, and alpha values of the pixel using data[pixelInfo], data[pixelInfo+1], data[pixelInfo+2], and data[pixelInfo+3], respectively.
  5. Finally, it creates and returns a UIColor object using these values.

And that’s it! We have successfully obtained the colour value of a specified pixel in a UIImage. Keep in mind that this method can be slow for large images, so it’s important to use it sparingly and optimize it as much as possible.


Suppose we have the following image.

The following snippet shows how to get the colour of a specific pixel at a given position on this image.

let image = UIImage(named: "img")!
let width = Int(image.size.width)
let height = Int(image.size.height)
print("Image size: ", "\(width) * \(height)")

if let colour = image.getPixelColor(pos: CGPoint(x: 0, y: 0)) {
    print("Top-left corner: \(colour)")
if let colour = image.getPixelColor(pos: CGPoint(x: width, y: 0)) {
    print("Top-right corner: \(colour)")
if let colour = image.getPixelColor(pos: CGPoint(x: width/2, y: height/2)) {
    print("Centre: \(colour)")


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top