demo下載鏈接:https://share.weiyun.com/5sjcVi5
項(xiàng)目主要包含兩個(gè)類 :GetScreenPointColor
和UIImage+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