【问题标题】:iOS -- detect the color of a pixel?iOS——检测像素的颜色?
【发布时间】:2011-06-04 17:52:56
【问题描述】:

例如,假设我想用屏幕坐标 (100, 200) 检测像素的颜色。有没有办法做到这一点?

编辑 -- 我现在不担心视网膜显示问题。

【问题讨论】:

    标签: ios colors screen pixel


    【解决方案1】:

    这可能不是最直接的路线,但您可以:

    1. 使用 UIGraphicsBeginImageContextWithOptions 抓取屏幕(参见 Apple Q&A QA1703 - “Screen Capture in UIKit Applications”)。

    2. 然后使用CGImageCreateWithImageInRect 抓取您需要的结果图像部分。

    3. 最后分析生成的图像。在这一点上它变得复杂了,但幸运的是,有一个现有的问题应该告诉你方法:How to get the RGB values for a pixel on an image on the iphone

    或者,有以下博客文章附带代码:What Color is My Pixel? Image based color picker on iPhone

    【讨论】:

    • @WilliamJockusch 我已经实现了这个,但是我想定期检测颜色(屏幕上只有 1 个像素)(每 0.04 秒使用计时器)创建整个屏幕的屏幕截图导致这个滞后我的应用程序...您没有碰巧找到一种更有效的内存效率方法,对吗?并为评论这么老的帖子道歉! ;)
    • @simonthumper 遗憾的是没有。如果您不需要抓住屏幕,您可能可以让事情变得更快。 (这实际上取决于图像数据的来源。)也就是说,如果您尝试解决的问题足够充分,我很想创建一个新问题(尽管提到您已经阅读/吸收了这个问题)不同。
    【解决方案2】:

    这里是怎么做的

    CGContextRef ctx;
    CGImageRef imageRef = [image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = malloc(height * width * 4);
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);
    
    //GET PIXEL FROM POINT
    int index = 4*((width*round(yCoor))+round(xCoor));
    
    int R = rawData[index];
    int G = rawData[index+1];
    int B = rawData[index+2];
    
    NSLog(@"%d   %d   %d", R, G, B);
    
    //IF YOU WANT TO ALTER THE PIXELS
    
    int byteIndex = 0;
    
    for(int ii = 0 ; ii < width * height ; ++ii)
    { 
        rawData[byteIndex] = (char)(newPixelValue);
        rawData[byteIndex+1] = (char)(newPixelValue);
        rawData[byteIndex+2] = (char)(newPixelValue);
    
        byteIndex += 4;
    }
    
    
    ctx = CGBitmapContextCreate(rawData,
                                CGImageGetWidth( imageRef ),
                                CGImageGetHeight( imageRef ),
                                8,
                                CGImageGetBytesPerRow( imageRef ),
                                CGImageGetColorSpace( imageRef ),
                                kCGImageAlphaPremultipliedLast ); 
    
    imageRef = CGBitmapContextCreateImage(ctx);
    UIImage* rawImage = [UIImage imageWithCGImage:imageRef];  
    
    CGContextRelease(ctx);  
    
    image = rawImage;  
    
    free(rawData);
    

    【讨论】:

    • 为什么要将索引乘以 4?因为它是每个像素 4 个字节?
    • 如何获取uiview的rgb颜色?
    【解决方案3】:

    试试这个,其中“self.m_imgvwSource”是根据您的需要的 uiview/uiimageview

    - (UIColor *) GetCurrentPixelColorAtPoint:(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.m_imgvwSource.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;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 2018-07-16
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多