如何检查一个UIView或一个UIImage的像素颜色? [英] How to check pixel color of a UIView or a UIImage?
问题描述
我想检查是否整个的UIView充满了一种颜色。我设法用code以下块得到的UIView的截图:
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];
有关imageFromView的方法实现如下:
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;
}
所以,从我个人理解,我现在的UIView的位图图像。我如何访问的UIImage的每个像素和检查,以确认它实际上是一种颜色(即的UIColor blackColor)?
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)?
推荐答案
如果你回到<一个href=\"http://stackoverflow.com/questions/10113737/ios-looping-over-the-pixels-of-an-image/10114115#10114115%20that%20answer\">that回答,下面的(未经测试)段应该让你开始:
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 == ...) {
}
}
}
}
这篇关于如何检查一个UIView或一个UIImage的像素颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!