我正在为我的第一个Mac OS X可可应用程序工作10.5+,我有一个CVImageBufferRef(使用QTKit捕获),我需要通过TCP Socket将此图像传输到客户端应用程序。客户端应用程序需要RGB值。所以这里是我目前正在做的(我目前的解决方案按需要工作,但使用了很多CPU)NSBitmapImageRep为高效率的RGB
CVImageBufferRef - > NSBitmapImageRep - > NSData - >然后通过TCP套接字传输NSData客户端应用程序和客户端一边我有以下代码来获取RGB:
UInt8 r,g,b
int width=320;
int height=240;
NSData *data; //read from TCP Socket
NSBitmapImageRep *bitmap=[[NSBitmapImageRep alloc] initWithData:data];
for (y=1;y<=height;y++) {
for(x=1;x<=width;x++){
NSColor *color=[bitmap colorAtX:x y:y];
// ^^ this line is actually a culprit and uses a lot of CPU
r=[color redComponent]*100;
g=[color greenComponent]*100;
b=[color blueComponent]*100;
NSLog(@"r:%d g:%d b:%d",r,g,b);
}
}
[bitmap release];
如果CVImageBufferRef可以转换为RGB值,这将是完美的一个数组,否则我需要一个有效的解决方案,以NSBitmapImageRep转换为RGB值。
您的示例代码有问题。你不使用“位图”,什么是“self.imageData”? – JWWalker
对不起,更正了代码,实际上在我的真实代码中,另一个线程将图像存储在ivar中,第二个线程访问imageData,使用NSLock维护线程安全。 –