【发布时间】:2010-03-05 12:48:43
【问题描述】:
我已经创建了一个 java 服务器,它可以截取屏幕截图、调整它们的大小,然后通过 TCP/IP 将它们发送到我的 iPhone 应用程序。然后应用程序使用 NSInputStream 收集传入的图像数据,使用字节缓冲区创建一个 NSMutableData 实例,然后创建一个 UIImage 对象以在 iPhone 上显示。屏幕共享,本质上。我收集图像数据的 iPhone 代码目前如下:
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent{
if(streamEvent == NSStreamEventHasBytesAvailable && [iStream hasBytesAvailable]){
uint8_t buffer[1024];
while([iStream hasBytesAvailable]){
NSLog(@"New Data");
int len = [iStream read:buffer maxLength:sizeof(buffer)];
[imgdata appendBytes:buffer length:len];
fullen=fullen+len;
/*Here is where the problem lies. What should be in this
if statement in order to make it test the last byte of
the incoming buffer, to tell if it is the End of Image marker
for the end of incoming JPEG file?
*/
if(buffer[len]=='FFD9'){
UIImage *img = [[UIImage alloc] initWithData:imgdata];
NSLog(@"NUMBER OF BYTES: %u", len);
image.image = img;
}
}
}
}
我的问题,如代码中的注释所示,是弄清楚何时停止收集 NSMutableData 对象中的数据,并使用该数据创建 UIImage。在传入字节中查找 JPEG 文件结束标记——图像结束 (EOI) 标记 (FFD9)——似乎是有意义的,因为在发送时图像将准备好显示。我该如何测试呢?我要么遗漏了有关数据如何存储的信息,要么遗漏了 JPEG 文件中的标记,但我们将非常感谢对此进行测试的任何帮助!
詹姆斯
【问题讨论】:
-
1)
buffer[len]超过了它读取的块的末尾,它只有len字节长(因此最后一个索引是len-1,并且 2)'FFD9'不是byte,为4个字节;您可能是指0xFFD9,它是 2 个字节,但是您将不得不以某种方式读取最后 2 个字节,而不仅仅是最后一个字节;然后你必须单独测试字节,或者不得不担心字节顺序
标签: iphone objective-c nsstream nsinputstream