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

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;
}
  1. 通过触摸点,判断该点是否在范围内,然后获取某点的颜色值
- (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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值