獲取圖片中某一點(diǎn)的顏色

demo下載鏈接:https://share.weiyun.com/5sjcVi5

項(xiàng)目主要包含兩個(gè)類 :GetScreenPointColorUIImage+GetPointColor

  • GetScreenPointColor 獲取觸摸點(diǎn)并截圖
  • UIImage+GetPointColor 獲取圖片某一點(diǎn)的顏色

GetScreenPointColor中包括3個(gè)方法:

  • getClickedColorWithTouches:withEvent:
    當(dāng)有一個(gè)或多個(gè)手指觸摸事件在當(dāng)前視圖或window窗體中響應(yīng)
  • getClickedPointWithTouches:withEvent:
    獲取當(dāng)前點(diǎn)坐標(biāo)
  • fullScreenshots:
    獲取截屏

GetScreenPointColor.h

定義枚舉:

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

定義屬性及方法:

@property (nonatomic, assign) Coordinate coordinate;
//當(dāng)有一個(gè)或多個(gè)手指觸摸事件在當(dāng)前視圖或window窗體中響應(yīng)
- (UIColor *)getClickedColorWithTouches:(NSSet *)touches withEvent:(UIEvent *)event;
//獲取當(dāng)前點(diǎn)坐標(biāo)
- (CGPoint)getClickedPointWithTouches:(NSSet *)touches withEvent:(UIEvent *)event;
//獲取截屏
-(UIImage *)fullScreenshots;

GetScreenPointColor.m

定義屬性

@property (nonatomic, strong) UIImage *screenImage;

getClickedColorWithTouches:withEvent:方法

//當(dāng)有一個(gè)或多個(gè)手指觸摸事件在當(dāng)前視圖或window窗體中響應(yīng)
- (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:方法

//獲取當(dāng)前點(diǎn)坐標(biāo)
- (CGPoint)getClickedPointWithTouches:(NSSet *)touches withEvent:(UIEvent *)event
{
    //返回與當(dāng)前接收者有關(guān)的所有的觸摸對(duì)象
    NSSet *allTouches = [event allTouches];
    //視圖中的所有對(duì)象
    UITouch *touch = [allTouches anyObject];
    
    CGPoint point = CGPointMake(0, 0);
    
    if (self.coordinate == coordinate_current)
    {
        //返回觸摸點(diǎn)在當(dāng)前坐標(biāo)系中的當(dāng)前坐標(biāo)
        point = [touch locationInView:[touch view]];
    }
    
    if (self.coordinate == coordinate_window)
    {
        //返回觸摸點(diǎn)在windows中的當(dāng)前坐標(biāo)
        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
{
    //判斷給定的點(diǎn)是否被一個(gè)CGRect包含
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point))
    {
        return nil;
    }
    
    //顏色空間DeviceRGB
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    /*
     data               指向要渲染的繪制內(nèi)存的地址。這個(gè)內(nèi)存塊的大小至少是(bytesPerRow*height)個(gè)字節(jié)
     width              bitmap的寬度,單位為像素
     height             bitmap的高度,單位為像素
     bitsPerComponent   內(nèi)存中像素的每個(gè)組件的位數(shù).例如,對(duì)于32位像素格式和RGB 顏色空間,你應(yīng)該將這個(gè)值設(shè)為8
     bytesPerRow        bitmap的每一行在內(nèi)存所占的比特?cái)?shù)
     colorspace         bitmap上下文使用的顏色空間
     bitmapInfo         指定bitmap是否包含alpha通道庆猫,像素中alpha通道的相對(duì)位置棋蚌,像素組件是整形還是浮點(diǎn)型等信息的字符串。
     */

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

運(yùn)行效果:https://share.weiyun.com/5Iewt73

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末璃诀,一起剝皮案震驚了整個(gè)濱河市弧可,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌劣欢,老刑警劉巖棕诵,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異凿将,居然都是意外死亡校套,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門牧抵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)笛匙,“玉大人,你說(shuō)我怎么就攤上這事犀变∩潘悖” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵弛作,是天一觀的道長(zhǎng)涕蜂。 經(jīng)常有香客問(wèn)我,道長(zhǎng)映琳,這世上最難降的妖魔是什么机隙? 我笑而不...
    開(kāi)封第一講書人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮萨西,結(jié)果婚禮上有鹿,老公的妹妹穿的比我還像新娘。我一直安慰自己谎脯,他們只是感情好葱跋,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著源梭,像睡著了一般娱俺。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上废麻,一...
    開(kāi)封第一講書人閱讀 51,737評(píng)論 1 305
  • 那天荠卷,我揣著相機(jī)與錄音,去河邊找鬼烛愧。 笑死油宜,一個(gè)胖子當(dāng)著我的面吹牛掂碱,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播慎冤,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼疼燥,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了蚁堤?” 一聲冷哼從身側(cè)響起醉者,我...
    開(kāi)封第一講書人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎违寿,沒(méi)想到半個(gè)月后湃交,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡藤巢,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年搞莺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片掂咒。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡才沧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出绍刮,到底是詐尸還是另有隱情温圆,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布孩革,位于F島的核電站岁歉,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏膝蜈。R本人自食惡果不足惜锅移,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望饱搏。 院中可真熱鬧非剃,春花似錦、人聲如沸推沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)鬓催。三九已至肺素,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間深浮,已是汗流浹背压怠。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留飞苇,地道東北人菌瘫。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像布卡,于是被迫代替她去往敵國(guó)和親雨让。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容