【问题标题】:How to check pixel color of a UIView or a UIImage?如何检查 UIView 或 UIImage 的像素颜色?
【发布时间】:2013-02-06 18:34:53
【问题描述】:

我正在尝试检查整个 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)?

【问题讨论】:

标签: objective-c uiview bitmap uiimage pixel


【解决方案1】:

如果您返回that answer,以下(未经测试的)sn-p 应该可以帮助您开始:

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

            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多