【发布时间】:2011-10-19 21:34:50
【问题描述】:
我正在为 10.5+ 开发我的第一个 mac osx cocoa 应用程序,其中我有一个 CVImageBufferRef(使用 QTKit 捕获),我需要通过 TCP 套接字将此图像传输到客户端应用程序。客户端应用程序需要 RGB 值。所以这就是我目前正在做的事情(我目前的解决方案可以根据需要工作,但会占用大量 CPU)
CVImageBufferRef --> NSBitmapImageRep --> NSData --> 然后通过 TCP Socket 将 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 值。
【问题讨论】:
-
您的示例代码有问题。你不用
bitmap,那self.imageData是什么? -
对不起,更正了代码,实际上在我的真实代码中,另一个线程将图像存储在 ivar 中,第二个线程正在访问 imageData,使用 NSLock 维护线程安全。
标签: macos cocoa qtkit nsbitmapimagerep