2

我正在尝试检查整个 UIView 是否填充了一种颜色。我设法使用以下代码块获取 UIView 的屏幕截图:

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];

imageFromView 的方法实现如下:

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

所以据我了解,我现在有一个 UIView 的位图图像。如何访问 UIImage 的每个像素并检查以验证它实际上是一种颜色(即 UIColor blackColor)?

4

1 回答 1

1

如果你回到那个答案,下面的(未经测试的)片段应该让你开始:

- (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 == ...) {

            }
        }
    }
}
于 2013-02-06T19:24:18.543 回答