获取图片中某一点的颜色

2018-11-20  本文已影响0人  黎明s

demo下载链接:https://share.weiyun.com/5sjcVi5

项目主要包含两个类 :GetScreenPointColorUIImage+GetPointColor

GetScreenPointColor中包括3个方法:


GetScreenPointColor.h

定义枚举:

typedef enum : NSUInteger {
    coordinate_window,
    coordinate_current,
} Coordinate;

定义属性及方法:

@property (nonatomic, assign) Coordinate coordinate;
//当有一个或多个手指触摸事件在当前视图或window窗体中响应
- (UIColor *)getClickedColorWithTouches:(NSSet *)touches withEvent:(UIEvent *)event;
//获取当前点坐标
- (CGPoint)getClickedPointWithTouches:(NSSet *)touches withEvent:(UIEvent *)event;
//获取截屏
-(UIImage *)fullScreenshots;

GetScreenPointColor.m

定义属性

@property (nonatomic, strong) UIImage *screenImage;

getClickedColorWithTouches:withEvent:方法

//当有一个或多个手指触摸事件在当前视图或window窗体中响应
- (UIColor *)getClickedColorWithTouches:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.coordinate = coordinate_window;
    CGPoint point = [self getClickedPointWithTouches:touches withEvent:event];
    
    self.screenImage = [self fullScreenshots];
    UIColor *currentColor = [self.screenImage colorAtPixel:point];
    return currentColor;
}

getClickedPointWithTouches:withEvent:方法

//获取当前点坐标
- (CGPoint)getClickedPointWithTouches:(NSSet *)touches withEvent:(UIEvent *)event
{
    //返回与当前接收者有关的所有的触摸对象
    NSSet *allTouches = [event allTouches];
    //视图中的所有对象
    UITouch *touch = [allTouches anyObject];
    
    CGPoint point = CGPointMake(0, 0);
    
    if (self.coordinate == coordinate_current)
    {
        //返回触摸点在当前坐标系中的当前坐标
        point = [touch locationInView:[touch view]];
    }
    
    if (self.coordinate == coordinate_window)
    {
        //返回触摸点在windows中的当前坐标
        point = [touch locationInView:[touch window]];
    }
    return point;
}

fullScreenshots方法

//获取截屏
-(UIImage *)fullScreenshots
{
    UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
    UIGraphicsBeginImageContext(screenWindow.frame.size);//全屏截图,包括window
    
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    
    return viewImage;
}

UIImage+GetPointColor.h

- (UIColor *)colorAtPixel:(CGPoint)point;

UIImage+GetPointColor.m

- (UIColor *)colorAtPixel:(CGPoint)point
{
    //判断给定的点是否被一个CGRect包含
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point))
    {
        return nil;
    }
    
    //颜色空间DeviceRGB
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    /*
     data               指向要渲染的绘制内存的地址。这个内存块的大小至少是(bytesPerRow*height)个字节
     width              bitmap的宽度,单位为像素
     height             bitmap的高度,单位为像素
     bitsPerComponent   内存中像素的每个组件的位数.例如,对于32位像素格式和RGB 颜色空间,你应该将这个值设为8
     bytesPerRow        bitmap的每一行在内存所占的比特数
     colorspace         bitmap上下文使用的颜色空间
     bitmapInfo         指定bitmap是否包含alpha通道,像素中alpha通道的相对位置,像素组件是整形还是浮点型等信息的字符串。
     */

    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * 1;
    NSUInteger bitsPerComponent = 8;
    unsigned char pixelData[4] = {0, 0, 0, 0};
    
    CGContextRef context = CGBitmapContextCreate(pixelData, 1, 1, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    
    CGColorSpaceRelease(colorSpace);
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    
    CGContextTranslateCTM(context, -point.x, point.y - self.size.height);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), self.CGImage);
    CGContextRelease(context);
    
    CGFloat red   = (CGFloat)pixelData[0] / 255.0f;
    CGFloat green = (CGFloat)pixelData[1] / 255.0f;
    CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;
    CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

在 ViewController 中的使用:

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImage *imgTest = [UIImage imageNamed:@"test.jpeg"];
    
    UIImageView *imgViewTest = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 300, 300)];
    imgViewTest.image = imgTest;
    imgViewTest.userInteractionEnabled = YES;
    [self.view addSubview:imgViewTest];
    
    self.viewShow = [[UIView alloc] initWithFrame:CGRectMake(50, 400, 100, 100)];
    self.viewShow.layer.borderColor = [UIColor redColor].CGColor;
    self.viewShow.layer.borderWidth = 1.0f;
    [self.view addSubview:self.viewShow];
    
    self.labX = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.viewShow.frame) + 5, self.viewShow.frame.origin.y, 200, self.viewShow.frame.size.height / 2)];
    [self.view addSubview:self.labX];
    self.labX.text = @"X:";
    
    self.labY = [[UILabel alloc] initWithFrame:CGRectMake(self.labX.frame.origin.x, CGRectGetMaxY(self.labX.frame), self.labX.frame.size.width, self.labX.frame.size.height)];
    [self.view addSubview:self.labY];
    self.labY.text = @"Y";
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    GetScreenPointColor *screen = [[GetScreenPointColor alloc] init];
    
    screen.coordinate = coordinate_window;
    CGPoint pointCurrent = [screen getClickedPointWithTouches:touches withEvent:event];
    
    UIColor *colorCurrent = [screen getClickedColorWithTouches:touches withEvent:event];
    
    self.viewShow.backgroundColor = colorCurrent;
    self.labX.text = [NSString stringWithFormat:@"X:%.2f",pointCurrent.x];
    self.labY.text = [NSString stringWithFormat:@"Y:%.2f",pointCurrent.y];
}

运行效果:https://share.weiyun.com/5Iewt73

上一篇下一篇

猜你喜欢

热点阅读