I tried to detect main colour by CIAreaMaximum. However I can solely get the white colour(rgb:255,255,255) in consequence irrespective of no matter image I enter. Do I misunderstand the operate of CIAreaMaximum? Any assist shall be appreciated.
struct ContentView: View {
let img = UIImage(named: "leaf") ?? UIImage()
var physique: some View {
VStack {
Picture(uiImage: img)
.resizable()
.scaledToFit()
Textual content("max")
Colour(uiColor: img.maxColor ?? .systemPink)
.body(peak: 50)
.border(.crimson)
}
.padding()
}
}
extension UIImage {
var maxColor: UIColor? {
guard let inputImage = CIImage(picture: self) else { return nil }
let extentVector = CIVector(x: inputImage.extent.origin.x, y: inputImage.extent.origin.y, z: inputImage.extent.dimension.width, w: inputImage.extent.dimension.peak)
guard let filter = CIFilter(title: "CIAreaMaximum", parameters: [kCIInputImageKey: inputImage, kCIInputExtentKey: extentVector]) else { return nil }
guard let outputImage = filter.outputImage else { return nil }
var bitmap = [UInt8](repeating: 0, depend: 4)
let context = CIContext(choices: [.workingColorSpace: kCFNull as Any])
context.render(outputImage, toBitmap: &bitmap, rowBytes: 4, bounds: CGRect(x: 0, y: 0, width: 1, peak: 1), format: .RGBA8, colorSpace: nil)
print("rgba",bitmap[0],bitmap[1],bitmap[2],bitmap[3])
return UIColor(crimson: CGFloat(bitmap[0])/255, inexperienced: CGFloat(bitmap[1])/255, blue: CGFloat(bitmap[2])/255, alpha: CGFloat(bitmap[3])/255)
}
}