Objective c 从iPhone上的PNG图像读取像素颜色值的简单方法?

Objective c 从iPhone上的PNG图像读取像素颜色值的简单方法?,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,有没有一种简单的方法来获得二维数组或类似的表示图像像素数据的东西 我有黑白PNG图像,我只想读取特定坐标下的颜色值。例如,20/100处的颜色值。您可以将png放入图像视图中,然后使用从图形上下文中获取像素值,您可以将图像绘制到其中。直接方法有点繁琐,但如下所示: 获取CoreGraphics图像 CGImageRef cgImage=image.cgImage 获取“数据提供者”,并从中获取数据。 NSData*d=[(id)CGDataProviderCopyData(CGImageGetD

有没有一种简单的方法来获得二维数组或类似的表示图像像素数据的东西


我有黑白PNG图像,我只想读取特定坐标下的颜色值。例如,20/100处的颜色值。

您可以将png放入图像视图中,然后使用从图形上下文中获取像素值,您可以将图像绘制到其中。

直接方法有点繁琐,但如下所示:

  • 获取CoreGraphics图像

    CGImageRef cgImage=image.cgImage

  • 获取“数据提供者”,并从中获取数据。
    NSData*d=[(id)CGDataProviderCopyData(CGImageGetDataProvider(cgImage))自动删除]

  • 找出数据的格式

    CGImageGetBitmapInfo()

    CGImageGetBitsPerComponent()

    CGImageGetBitsPerPixel()

    CGImageGetBytesPerRow()

  • 找出颜色空间(PNG支持灰度/RGB/调色板)

    CGImageGetColorSpace()

  • 间接方法是将图像绘制到上下文(请注意,如果需要任何保证,可能需要指定上下文的字节顺序)并读取字节。
    如果只需要单个像素,则使用右矩形将图像绘制到1x1上下文可能会更快
    (类似于
    (CGRect){{-x,-y},{imgWidth,imgHeight}
    )。

    这将为您处理颜色空间转换。如果您只需要亮度值,请使用灰度上下文。

    UIImage上的此类别可能会有所帮助

    #导入
    #导入“UIImage+ColorAtPixel.h”
    @实现UIImage(ColorAtPixel)
    -(UIColor*)颜色像素:(CGPoint)点{
    //如果点位于图像坐标之外,则取消
    如果(!CGRectContainsPoint(CGRectMake(0.0f,0.0f,self.size.width,self.size.height),点)){
    返回零;
    }
    //创建1x1像素字节数组和位图上下文以将像素绘制到其中。
    //参考:http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage
    NSInteger pointX=trunc(point.x);
    NSInteger pointY=trunc(point.y);
    CGImageRef cgImage=self.cgImage;
    NSU整数宽度=CGImageGetWidth(cgImage);
    NSU整数高度=CGImageGetHeight(cgImage);
    CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
    int字节/像素=4;
    int bytesPerRow=字节像素*1;
    NSU整数比特分量=8;
    无符号字符像素数据[4]={0,0,0,0};
    CGContextRef context=CGBitmapContextCreate(像素数据,
    1.
    1.
    比特组件,
    拜特斯佩罗,
    色彩空间,
    KCGIMAGEAlphaPremultipledLast | kCGBitmapByteOrder32Big);
    CGCOLORSPACTERELEASE(色彩空间);
    CGContextSetBlendMode(上下文,kCGBlendModeCopy);
    //在位图上下文中绘制我们感兴趣的像素
    CGContextTranslateCm(上下文,-pointX,-pointY);
    CGContextDrawImage(上下文,CGRectMake(0.0f,0.0f,(CGFloat)宽度,(CGFloat)高度),cgImage);
    CGContextRelease(上下文);
    //将颜色值[0..255]转换为浮点数[0.0..1.0]
    CGFloat red=(CGFloat)像素数据[0]/255.0f;
    CGFloat绿色=(CGFloat)像素数据[1]/255.0f;
    CGFloat蓝色=(CGFloat)像素数据[2]/255.0f;
    CGFloat alpha=(CGFloat)像素数据[3]/255.0f;
    返回[UIColor COLOR WITHRED:红绿色:绿蓝色:蓝色阿尔法:阿尔法];
    }
    @结束
    
    一个为你做这件事的课程,并解释了:

    #import <CoreGraphics/CoreGraphics.h>
    
    #import "UIImage+ColorAtPixel.h"
    
    @implementation UIImage (ColorAtPixel)
    
    - (UIColor *)colorAtPixel:(CGPoint)point {
        // Cancel if point is outside image coordinates
        if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) {
            return nil;
        }
    
    
        // Create a 1x1 pixel byte array and bitmap context to draw the pixel into.
        // Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage
        NSInteger pointX = trunc(point.x);
        NSInteger pointY = trunc(point.y);
        CGImageRef cgImage = self.CGImage;
        NSUInteger width = CGImageGetWidth(cgImage);
        NSUInteger height = CGImageGetHeight(cgImage);
        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);
    
        // Draw the pixel we are interested in onto the bitmap context
        CGContextTranslateCTM(context, -pointX, -pointY);
        CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
        CGContextRelease(context);
    
        // Convert color values [0..255] to floats [0.0..1.0]
        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];
    }
    
    @end