Detect a specific UIColor in a UIImage

I working on a drawing app using UIKit and I am trying to create a function that receives a UIImageView as an argument detects the color and then return that UIImageView updated. For example the UIImageView.image will contains a set of UIColors s and my detectColor will check if the image contains .systemGrey then will take any pixel that contains that specific color and change it to .systemPink and return the UIImageView updated

For a beter explation here is an example of my function with pseudo code:

func updateColor(_ imageView: UIImageView ) -> UIImageView {
//.. initialize imageView
//... if imageView.image color has pixels in .systemGrey color then 
//...  change that color to .systemPink
//...  return updated imageView 
}

Like I said I only want to change that specific color taking into account that in some cases my UIImageView.image multiple UIColors .Any Ideas, examples or suggestions will be much appreciated. Thanks!.

Replies

If I understand, you have to loop all the pixels in the image, get its colour.

This may help: https://stackoverflow.com/questions/33768066/get-pixel-data-as-array-from-uiimage-cgimage-in-swift

Take care, .systemgrey is not an absolute colour, it changes with dark / light.

  • Nice that gives me an idea of how to deal with my function. Thanks @Claude31

Add a Comment