如何获得UIView中像素的颜色?

0x9*_*x90 33 iphone pixel colors uiview

我正在尝试开发一个小应用程序.在其中,我需要检测UIView中像素的颜色.我有一个CGPoint定义我需要的像素.使用CoreAnimation更改UIView的颜色.

我知道有一些复杂的方法可以从UIImages中提取颜色信息.但是我找不到UIViews的解决方案.

在伪代码我正在寻找类似的东西

pixel = [view getPixelAtPoint:myPoint];
UIColor *mycolor = [pixel getColor];
Run Code Online (Sandbox Code Playgroud)

任何输入都非常感谢.

iva*_*oid 78

这是更有效的解决方案:

// UIView+ColorOfPoint.h
@interface UIView (ColorOfPoint)
- (UIColor *) colorOfPoint:(CGPoint)point;
@end

// UIView+ColorOfPoint.m
#import "UIView+ColorOfPoint.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIView (ColorOfPoint)

- (UIColor *) colorOfPoint:(CGPoint)point
{
    unsigned char pixel[4] = {0};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

    [self.layer renderInContext:context];

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    //NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]);

    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];

    return color;
}

@end
Run Code Online (Sandbox Code Playgroud)

链接到文件:https://github.com/ivanzoid/ikit/tree/master/UIView+ColorOfPoint


War*_*ton 22

一个迅速的版本的ivanzoid的回答

斯威夫特3

extension CALayer {

    func colorOfPoint(point:CGPoint) -> CGColor {

        var pixel: [CUnsignedChar] = [0, 0, 0, 0]

        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)

        let context = CGContext(data: &pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)

        context!.translateBy(x: -point.x, y: -point.y)

        self.render(in: context!)

        let red: CGFloat   = CGFloat(pixel[0]) / 255.0
        let green: CGFloat = CGFloat(pixel[1]) / 255.0
        let blue: CGFloat  = CGFloat(pixel[2]) / 255.0
        let alpha: CGFloat = CGFloat(pixel[3]) / 255.0

        let color = UIColor(red:red, green: green, blue:blue, alpha:alpha)

        return color.cgColor
    }
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特2

extension CALayer {

    func colorOfPoint(point:CGPoint)->CGColorRef
    {
        var pixel:[CUnsignedChar] = [0,0,0,0]

        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)

        let context = CGBitmapContextCreate(&pixel, 1, 1, 8, 4, colorSpace, bitmapInfo.rawValue)

        CGContextTranslateCTM(context, -point.x, -point.y)

        self.renderInContext(context!)

        let red:CGFloat = CGFloat(pixel[0])/255.0
        let green:CGFloat = CGFloat(pixel[1])/255.0
        let blue:CGFloat = CGFloat(pixel[2])/255.0
        let alpha:CGFloat = CGFloat(pixel[3])/255.0

        let color = UIColor(red:red, green: green, blue:blue, alpha:alpha)

        return color.CGColor
    }

}
Run Code Online (Sandbox Code Playgroud)

基于底层CALayer但很容易翻译回来UIView.

  • 我试图使用它,但它只返回零.所以我总是得到白色.不幸的是,我的编程技巧并不那么先发现自己.谢谢 (2认同)

Lou*_*arg 8

这是非常可怕和缓慢的.基本上,您使用您分配的后备存储创建位图上下文,以便您可以读取内存,然后在上下文中渲染视图层并读取ram中的相应点.

如果您已经知道如何为图像执行此操作,则可以执行以下操作:

- (UIImage *)imageForView:(UIView *)view {
  UIGraphicsBeginImageContext(view.frame.size);
  [view.layer renderInContext: UIGraphicsGetCurrentContext()];
  UIImage *retval = UIGraphicsGetImageFromCurrentImageContext(void);
  UIGraphicsEndImageContext();

  return retval;
}
Run Code Online (Sandbox Code Playgroud)

然后,您将获得一个可以获取像素数据的图像.我确信你处理图像的机制无论如何都要将它们渲染成一个上下文,所以你可以将它与它合并并分解实际的图像创建.因此,如果您采取这种方法并删除加载图像的位并将其替换为上下文渲染:

[view.layer renderInContext: UIGraphicsGetCurrentContext()];
Run Code Online (Sandbox Code Playgroud)

你应该好.


WoN*_*DeR 8

斯威夫特 44.2。使用运行 iOS 12.1 的 XCode 10、iOS 12、模拟器和 iPhone 6+ 进行测试。

这段代码工作正常。

extension UIView {
    func colorOfPoint(point: CGPoint) -> UIColor {
        let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)

        var pixelData: [UInt8] = [0, 0, 0, 0]

        let context = CGContext(data: &pixelData, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)

        context!.translateBy(x: -point.x, y: -point.y)

        self.layer.render(in: context!)

        let red: CGFloat = CGFloat(pixelData[0]) / CGFloat(255.0)
        let green: CGFloat = CGFloat(pixelData[1]) / CGFloat(255.0)
        let blue: CGFloat = CGFloat(pixelData[2]) / CGFloat(255.0)
        let alpha: CGFloat = CGFloat(pixelData[3]) / CGFloat(255.0)

        let color: UIColor = UIColor(red: red, green: green, blue: blue, alpha: alpha)

        return color
    }
}
Run Code Online (Sandbox Code Playgroud)