Objective c Cocoa OS X中的核心图形:从CGImageRef获取原始数据?

Objective c Cocoa OS X中的核心图形:从CGImageRef获取原始数据?,objective-c,c,macos,cocoa,core-graphics,Objective C,C,Macos,Cocoa,Core Graphics,我发现了两种从CGImageRef获取原始数据的方法: 第一种方法: unsigned char *CGImageCopyRawData(CGImageRef image, size_t *rawDataLength) { CGDataProviderRef dataProvider = CGImageGetDataProvider(image); CFDataRef data = CGDataProviderCopyData(dataProvider); if(ra

我发现了两种从CGImageRef获取原始数据的方法:

第一种方法:

 unsigned char *CGImageCopyRawData(CGImageRef image, size_t *rawDataLength) {

    CGDataProviderRef dataProvider = CGImageGetDataProvider(image);
    CFDataRef data = CGDataProviderCopyData(dataProvider);
    if(rawDataLength) *rawDataLength = (size_t) CFDataGetLength(data);
    CFRelease(dataProvider);
    CFRelease(data); 
    return CFDataGetBytePtr(data);
}
第二种方法:

static unsigned char *CGImageCopyPixelData(CGImageRef image, CGColorSpaceRef colorSpace, uint32_t bitmap_info, size_t *pixelDataLength) {

    unsigned char *pixelData = 0;

    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);

    // allocate image pixel components array, where each component takes one byte (unsigned char)
    size_t dataLength = width*height*COMPONENTS_PER_PIXEL*BYTES_PER_COMPONENT;
    pixelData = (unsigned char*) malloc(sizeof(unsigned char) * (dataLength));
    if(pixelData == 0) {
        *pixelDataLength = 0;
        return 0;
    }

    // draw image using core graphics context in frame buffer
    CGContextRef context = CGBitmapContextCreate(pixelData, width, height, BITS_PER_COMPONENT,
                                                 COMPONENTS_PER_PIXEL * width, colorSpace, bitmap_info);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), image);
    CGContextRelease(context);

    if(pixelDataLength) *pixelDataLength = dataLength;
    return pixelData;
}
我听说第一种方法返回原始数据,但不保证颜色空间、每个像素的组件数、每个组件的位数、字节顺序、大端、小端

但是考虑到CGImageRef只是
CGDisplayCreateImage(mainDisplayId)
的屏幕截图,我可以假设这将是位图掩码
KCGimageAlphaPremultipledFirst | kCGBitmapByteOrder32Little

但是这个to方法返回不同的结果,它不是不同的字节顺序,而是在检查图形程序(如Adobe Photoshop)中的十六进制颜色时,值略有不同,但颜色相似:

第一个输出是:

[F8 E0 D2 FF] [F8 E0 D2 FF] [F8 E0 D2 FF] [F8 E0 D2 FF] [F8 E1 D2 FF] [F8 E1 D2 FF] [F8 E1 D2 FF] [F8 E1 D2 FF] [F8 E1 D2 FF] [F8 E1 D2 FF] [F8 E1 D2 FF]...
[F7 E1 D4 FF] [F7 E1 D4 FF] [F7 E1 D4 FF] [F7 E1 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF]... 
第二个输出是:

[F8 E0 D2 FF] [F8 E0 D2 FF] [F8 E0 D2 FF] [F8 E0 D2 FF] [F8 E1 D2 FF] [F8 E1 D2 FF] [F8 E1 D2 FF] [F8 E1 D2 FF] [F8 E1 D2 FF] [F8 E1 D2 FF] [F8 E1 D2 FF]...
[F7 E1 D4 FF] [F7 E1 D4 FF] [F7 E1 D4 FF] [F7 E1 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF] [F7 E2 D4 FF]... 

正如你们所看到的,这里有一点不同,比如一些位集合或未集合,可能是C中有符号/无符号字符的东西

一,。您的第一个代码示例包含内存泄漏。2.您的第二个代码示例返回的是
CGImageRef
,而不是原始数据指针。好的,我复制了错误的方法:)我现在已更新。1。您的第一个代码示例包含内存泄漏。2.您的第二个代码示例返回的是
CGImageRef
,而不是原始数据指针。好的,我复制了错误的方法:)我现在已经更新了。