此代码应该从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);
最后三行可以解决这个问题,但是编译器表示不会将x
和y
用作float
。所以我将它们投放到int
,但现在它说了
指向不完整类型const结构的指针__CFData
我该如何解决?
答案 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);