I'm trying to determine if a drawing currently is all white. The solution I could come up with was to scale down the image, then check pixel by pixel if it's white and return NO as soon as it finds a pixel that is not white.
It works, but I have a gut feeling it could be done in a more performant way. Here's the code:
- (BOOL)imageIsAllWhite:(UIImage *)image {
CGSize size = CGSizeMake(100.0f, 100.0f);
UIImageView *imageView = [[UIImageView alloc] initWithImage:[image scaledImageWithSize:size]];
unsigned char pixel[4 * (int)size.width * (int)size.height];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef cgContext = CGBitmapContextCreate(
pixel,
(size_t)size.width,
(size_t)size.height,
8,
(size_t)(size.width * 4),
colorSpace,
kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(cgContext, 0, 0);
[imageView.layer renderInContext:cgContext];
CGContextRelease(cgContext);
CGColorSpaceRelease(colorSpace);
for (int i = 0; i < sizeof(pixel); i = i + 4) {
if(!(pixel[i] == 255 && pixel[i+1] == 255 && pixel[i+2] == 255)) {
return NO;
}
}
return YES;
}
Any ideas for improvement?
Please following code for check whether UIImage is White color
Calling Functions