我想开发一个小应用程序。 在这里面,我需要检测的UIView内的像素的颜色。 我有一个CGPoint确定我所需要的像素。 UIView的的颜色是使用CoreAnimation改变。
我知道有一些复杂的方式来提取UIImages颜色信息。 但是我无法找到UIViews的解决方案。
伪码我在寻找类似
pixel = [view getPixelAtPoint:myPoint];
UIColor *mycolor = [pixel getColor];
任何输入不胜感激。
我想开发一个小应用程序。 在这里面,我需要检测的UIView内的像素的颜色。 我有一个CGPoint确定我所需要的像素。 UIView的的颜色是使用CoreAnimation改变。
我知道有一些复杂的方式来提取UIImages颜色信息。 但是我无法找到UIViews的解决方案。
伪码我在寻找类似
pixel = [view getPixelAtPoint:myPoint];
UIColor *mycolor = [pixel getColor];
任何输入不胜感激。
这是很可怕的和缓慢的。 基本上你创建你分配,以便可以读出存储器中的后备存储器的位图的上下文,则呈现在上下文的观点层和读取在RAM中的相应点。
如果你知道如何做到这一点的图像已经可以做这样的事情:
- (UIImage *)imageForView:(UIView *)view {
UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *retval = UIGraphicsGetImageFromCurrentImageContext(void);
UIGraphicsEndImageContext();
return retval;
}
然后你会得到一个图像,你可以得到的像素数据。 我相信你有处理图像的机构包括使它们成为背景,所以你可以合并与此,并分解出的实际图像创作。 所以,如果你需要,可以和删除,您加载图像和与上下文替换位渲染:
[view.layer renderInContext: UIGraphicsGetCurrentContext()];
你应该是不错的。
这是更有效的解决方案:
// UIView+ColorOfPoint.h
@interface UIView (ColorOfPoint)
- (UIColor *) colorOfPoint:(CGPoint)point;
@end
// UIView+ColorOfPoint.m
#import "UIView+ColorOfPoint.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIView (ColorOfPoint)
- (UIColor *) colorOfPoint:(CGPoint)point
{
unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, -point.x, -point.y);
[self.layer renderInContext:context];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
//NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]);
UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];
return color;
}
@end
链接到文件: https://github.com/ivanzoid/ikit/tree/master/UIView+ColorOfPoint
ivanzoid的答案的swift'ified版本
斯威夫特3
extension CALayer {
func colorOfPoint(point:CGPoint) -> CGColor {
var pixel: [CUnsignedChar] = [0, 0, 0, 0]
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
let context = CGContext(data: &pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
context!.translateBy(x: -point.x, y: -point.y)
self.render(in: context!)
let red: CGFloat = CGFloat(pixel[0]) / 255.0
let green: CGFloat = CGFloat(pixel[1]) / 255.0
let blue: CGFloat = CGFloat(pixel[2]) / 255.0
let alpha: CGFloat = CGFloat(pixel[3]) / 255.0
let color = UIColor(red:red, green: green, blue:blue, alpha:alpha)
return color.cgColor
}
}
斯威夫特2
extension CALayer {
func colorOfPoint(point:CGPoint)->CGColorRef
{
var pixel:[CUnsignedChar] = [0,0,0,0]
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
let context = CGBitmapContextCreate(&pixel, 1, 1, 8, 4, colorSpace, bitmapInfo.rawValue)
CGContextTranslateCTM(context, -point.x, -point.y)
self.renderInContext(context!)
let red:CGFloat = CGFloat(pixel[0])/255.0
let green:CGFloat = CGFloat(pixel[1])/255.0
let blue:CGFloat = CGFloat(pixel[2])/255.0
let alpha:CGFloat = CGFloat(pixel[3])/255.0
let color = UIColor(red:red, green: green, blue:blue, alpha:alpha)
return color.CGColor
}
}
基于底层周围CALayer
但容易翻译回UIView
。
修正了一些小错误
- (UIImage *)imageForView:(UIView *)view {
UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *retval = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return retval;
}
此外,添加
#import <QuartzCore/QuartzCore.h>
将您的文件,以避免警告消息。
与找到的代码结合您的代码在这里完美的作品。