I got this method that counts white pixels in a UIImage, I need to go through all the pixels to increase the counter with each white pixel that I find. I´m trying to improve the performance of it but I don´t find a better approach. Any ideas?
func whitePixelCount() -> Int {
let width = Int(image.size.width)
let height = Int(image.size.height)
var counter = 0
for x in 0..<(width*scale) {
for y in 0..<(height*scale) {
// We multiply per 4 because of the 4 channels, RGBA, but later we just use the Alpha
let pixelIndex = (width * y + x) * 4
if pointer[pixelIndex + Component.alpha.rawValue] == 255 {
counter += 1
}
}
}
return counter
}
Component.alpha.rawValueis equal to3scaleisInt(image.scale)pointercomes from:guard let cfdata = self.image.cgImage?.dataProvider?.data, let pointer = CFDataGetBytePtr(cfdata) else { return nil }