首页 > 系统 > iOS > 正文

iOS颜色摄合器,获取图片某点的颜色值

2019-11-07 23:52:45
字体:
来源:转载
供稿:网友

iOS颜色摄合器,获取图片某点的颜色值

版权声明:本文为博主原创文章

1.新建一个继承于UIImageView的类-YWColorByImageView 2.重写set方法,并通过上下图文创建实际对应像素的image

/** 重设image @param image image */- (void)setImage:(UIImage *)image { UIImage *temp = [self imageForResizeWithImage:image resize:CGSizeMake(self.frame.size.width, self.frame.size.width)]; [super setImage:temp];}/** 通过上下图文创建self对应像素的image @param picture 要加载的图片 @param resize 实际self的大小 @return 返回self对应像素的大小的image */- (UIImage *)imageForResizeWithImage:(UIImage *)picture resize:(CGSize)resize { CGSize imageSize = resize; //CGSizeMake(25, 25) /** UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale); 1.参数size为新创建的位图上下文的大小 2.opaque—透明开关,如果图形完全不用透明,设置为YES以优化位图的存储 3.scale—–缩放因子 iphone 4是2.0,其他是1.0;实际上设为0后,系统就会自动设置正确的比例了。 */ UIGraphicsBeginImageContextWithOptions(imageSize, NO,0.0); CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height); [picture drawInRect:imageRect]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); return image;}通过触摸点,判断该点是否在范围内,然后获取某点的颜色值- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location_point = [touch locationInView:self]; if ([YWDistanceTool getlayer:location_point withRadius:self.bounds.size.width/2]) { UIColor *color = [UIColor colorAtPixel:location_point withImage:self.image]; if (self.pointColorBlock) { self.pointColorBlock(color); } if ([self.delegate respondsToSelector:@selector(getCurrentColor:)]) { [self.delegate getCurrentColor:color]; } }}- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location_point = [touch locationInView:self]; if ([YWDistanceTool getlayer:location_point withRadius:self.bounds.size.width/2]) { UIColor *color = [UIColor colorAtPixel:location_point withImage:self.image]; if (self.pointColorBlock) { self.pointColorBlock(color); } if ([self.delegate respondsToSelector:@selector(getCurrentColor:)]) { [self.delegate getCurrentColor:color]; } }}- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location_point = [touch locationInView:self]; if ([YWDistanceTool getlayer:location_point withRadius:self.bounds.size.width/2]) { UIColor *color = [UIColor colorAtPixel:location_point withImage:self.image]; if (self.pointColorBlock) { self.pointColorBlock(color); } if ([self.delegate respondsToSelector:@selector(getCurrentColor:)]) { [self.delegate getCurrentColor:color]; } }}/** 获取图片某一点的颜色 @param point 点击图片的某一点 @param image 图片 @return 图片某点的颜色值 */+ (UIColor *)colorAtPixel:(CGPoint)point withImage:(UIImage *)image { // 判断给定的点是否被一个CGRect包含 if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), point)) { return nil; } // trunc(n1,n2),n1表示被截断的数字,n2表示要截断到那一位。n2可以是负数,表示截断小数点前。注意,TRUNC截断不是四舍五入。 // TRUNC(15.79)---15 // trunc(15.79,1)--15.7 NSInteger pointX = trunc(point.x); NSInteger pointY = trunc(point.y); CGImageRef cgImage = image.CGImage; NSUInteger width = image.size.width; NSUInteger height = image.size.height; // 创建色彩标准 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 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, -pointX, pointY-(CGFloat)height); CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), 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];}

源码请转:https://github.com/flyOfYW/YWDemo.git


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表