UIImageView-GetColor

RGB palette for iOS get color from UIImageView 可以从指定点获取颜色

Stars
6

UIImageView+GetColor

![](Resources/2016-05-20 21_00_39.gif)

,UIImageView+GetColor/.

**** ATKit

**** @AesirTitan

1. RGBA

- (void)at_getRed:(nullable CGFloat *)red green:(nullable CGFloat *)green blue:(nullable CGFloat *)blue alpha:(nullable CGFloat *)alpha withPoint:(CGPoint)point;
// get red green blue alpha with point
- (void)at_getRed:(nullable CGFloat *)red green:(nullable CGFloat *)green blue:(nullable CGFloat *)blue alpha:(nullable CGFloat *)alpha withPoint:(CGPoint)point{
    // ==================== [ filter ] ==================== //
    // frame of image
    const CGRect imageFrame = CGRectMake(0.0f, 0.0f,
                                         self.frame.size.width, self.frame.size.height);
    // Cancel if point is outside image coordinates
    if (!CGRectContainsPoint(imageFrame, point)) {
        return;
    }
    
    // ==================== [ function ] ==================== //
    // Create RGB color space
    const CGColorSpaceRef colorSpaceRGB  = CGColorSpaceCreateDeviceRGB();
    unsigned char pixelData[4] = { 0, 0, 0, 0 };
    CGContextRef context = CGBitmapContextCreate(pixelData,1,1,8,4,colorSpaceRGB,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpaceRGB);
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    // Draw the pixel we are interested in onto the bitmap context
    CGContextTranslateCTM(context, -point.x, point.y- self.frame.size.height);
    CGContextDrawImage(context, imageFrame, self.image.CGImage);
    CGContextRelease(context);
    
    // Convert color values [0..255] to floats [0.0..1.0]
    *red   = (CGFloat)pixelData[0] / 255.0f;
    *green = (CGFloat)pixelData[1] / 255.0f;
    *blue  = (CGFloat)pixelData[2] / 255.0f;
    *alpha = (CGFloat)pixelData[3] / 255.0f;
    
}

copyUIImageViewCategory

// imageViewpoint
CGFloat r,g,b,a;
[imageView at_getRed:&r green:&g blue:&b alpha:&a withPoint:point];
// r/g/b/a

2. RGBAblock

block

- (void)at_getRGBAWithPoint:(CGPoint)point completion:(void(^)(CGFloat red,CGFloat green,CGFloat blue,CGFloat alpha))completion;
// imageViewpoint
[imageView at_getRGBAWithPoint:point completion:^(CGFloat red,CGFloat green,CGFloat blue,CGFloat alpha){
  // red/green/blue/alpha
}];

blockred/green/blue/alpha

3. RGBAblock

- (void)at_getRGBAFromCircleWithPoint:(CGPoint)point completion:(void (^)(CGFloat red,CGFloat green,CGFloat blue,CGFloat alpha))completion;
// imageViewpoint
[imageView at_getRGBAFromCircleWithPoint:point completion:^(CGFloat red,CGFloat green,CGFloat blue,CGFloat alpha){
  // red/green/blue/alpha
}];

2block****

blockrgba

block

UIColorRGBA

4. UIColor

- (nullable UIColor *)at_getColorWithPoint:(CGPoint)point;
// imageViewpoint
UIColor *color = [imageView at_getColorWithPoint:point];
// UIColor

5. UIColorblock

- (void)at_getColorWithPoint:(CGPoint)point completion:(void(^)(UIColor *color))completion;
// imageViewpoint
[imageView at_getColorWithPoint:point completion:^(UIColor *color){
  // color
}];

4UIImageViewblock

6. UIColorblock

- (void)at_getColorFromCircleWithPoint:(CGPoint)point completion:(void (^)(UIColor *color))completion;
// imageViewpoint
[imageView at_getColorFromCircleWithPoint:point completion:^(UIColor *color){
  // color
}];

Demo

GitHub Star

Badges
Extracted from project README
License MIT Carthage compatible CocoaPods CocoaPods Support
Related Projects