2

I am trying to check if an entire UIView is filled with one color. I managed to get a screenshot of the UIView using the following block of code:

UIImage *image = [self imageFromView];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSData *myImageData = UIImagePNGRepresentation(image);
[fileManager createFileAtPath:@"/Users/{username}/Desktop/myimage.png" contents:myImageData attributes:nil];
[imageData writeToFile:@"/testImage.jpg" atomically:YES];

The method implementation for imageFromView is the following:

- (UIImage*)imageFromView{  
UIImage *bitmapImage;
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
bitmapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return bitmapImage; 
}

So from what I understand, I now have a bitmap image of the UIView. How do I access each pixel of the UIImage and check to verify it is in fact one color (i.e. UIColor blackColor)?

David
  • 583
  • 1
  • 10
  • 27
  • [this](http://stackoverflow.com/questions/448125/how-to-get-pixel-data-from-a-uiimage-cocoa-touch-or-cgimage-core-graphics) –  Feb 06 '13 at 18:39
  • This Stack Overflow answer might help [1]: http://stackoverflow.com/questions/10113737/ios-looping-over-the-pixels-of-an-image/10114115#10114115 – verec Feb 06 '13 at 18:39
  • I've stumbled upon those moments ago but it did not clear up my understanding as to how to tackle this problem. – David Feb 06 '13 at 18:53

1 Answers1

1

If you go back to that answer, the following (untested) snippet should get you started:

- (BOOL) checkForUniqueColor: (UIImage *) image {
    CGImageInspection * inspector = 
        [CGImageInspection imageInspectionWithCGImage: [image CGImage]] ;

    for (CGFloat x = 0 ; x < image.size.width ; x += 1.0f) {
        for (CGFloat y = 0 ; y < image.size.height ; y += 1.0f) {
            CGFloat red ;
            CGFloat green ;
            CGFloat blue ;
            CGFloat alpha ;

            [inspector colorAt: (CGPoint) {x, y}
                           red: &red
                         green: &green
                          blue: &blue
                         alpha: &alpha
            ] ;

            if (red == ... && green == ... && blue == ... && alpha == ...) {

            }
        }
    }
}
Community
  • 1
  • 1
verec
  • 5,224
  • 5
  • 33
  • 40