从CFData读取像素字节:对指向不完整类型的指针进行算术运算

时间:2014-01-06 00:07:06

标签: ios objective-c compiler-errors core-foundation bytebuffer

此代码应该从CGImageRef

开始获取每个像素的值
UIImage* image = [UIImage imageNamed:@"mask.bmp"];
CGImageRef aCGImageRef = image.CGImage;

CFDataRef rawData = CGDataProviderCopyData(CGImageGetDataProvider(aCGImageRef));
UInt8 * buf = (UInt8 *) CFDataGetBytePtr(rawData);
int length = CFDataGetLength(rawData);

CFRelease(rawData);

int no_of_channels = 3;
int image_width = SCREEN_WIDTH();

unsigned long row_stride = image_width * no_of_channels; // 960 bytes in this case
unsigned long x_offset = x * no_of_channels;

/* assuming RGB byte order (as opposed to BGR) */
UInt8 r = *(rawData + row_stride * y + x_offset );
UInt8 g = *(rawData + row_stride * y + x_offset + 1);
UInt8 b = *(rawData + row_stride * y + x_offset + 2);

最后三行可以解决这个问题,但是编译器表示不会将xy用作float。所以我将它们投放到int,但现在它说了

  

指向不完整类型const结构的指针__CFData

我该如何解决?

1 个答案:

答案 0 :(得分:3)

您希望对字节指针本身进行算术运算,而不是对CFData结构(将字节作为成员)进行算术运算。这意味着使用上面的buf变量:

UInt8 r = *(buf + row_stride * y + x_offset );
UInt8 g = *(buf + row_stride * y + x_offset + 1);
UInt8 b = *(buf + row_stride * y + x_offset + 2);