在iPhone上接收图像(TCP客户端)

我正在编程iPhone上的客户端。 我想发送一些string,并从服务器接收图像。

我发现这个教程(http://www.devx.com/wireless/Article/43551),它已经非常有用。 它可以工作,如果我想接收string,但现在我想要接收一个图像,我不能使它的工作。

当iPhone收到数据时,这是我的代码。 这是一个混合与教程和JeremyP这个答案(http://stackoverflow.com/questions/4613218):

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode { switch(eventCode) { case NSStreamEventHasBytesAvailable: { if (data == nil) { data = [[NSMutableData alloc] init]; } uint8_t buf[102400]; unsigned int len = 0; len = [(NSInputStream *)stream read:buf maxLength:102400]; if(len) { [data appendBytes:(const void *)buf length:len]; // Recibir img uint8_t size; // Let's make sure size is an explicit width. NSInteger totalBytesRead = 0; NSInteger bytesRead = [iStream read: &size maxLength: sizeof size]; while (bytesRead > 0 && totalBytesRead + bytesRead < sizeof size) { totalBytesRead+= bytesRead; bytesRead = [iStream read: &size + totalBytesRead maxLength: (sizeof size) - totalBytesRead]; } if (bytesRead >= 0) { totalBytesRead += bytesRead; } else { // read failure, report error and bail } if (totalBytesRead < sizeof size) { // connection closed before we got the whole size, report and bail } size = ntohl(size); // assume wire protocol uses network byte ordering NSMutableData* buffer = [[NSMutableData alloc] initWithLength: size]; totalBytesRead = 0; bytesRead = [iStream read: [buffer mutableBytes] maxLength: size]; while (bytesRead > 0 && totalBytesRead + bytesRead < size) { totalBytesRead+= bytesRead; bytesRead = [iStream read: [buffer mutableBytes] + totalBytesRead maxLength: size - totalBytesRead]; } if (bytesRead >= 0) { totalBytesRead += bytesRead; } else { // read failure, report error and bail (not forgetting to release buffer) } if (totalBytesRead < size) { // connection closed before we got the whole image, report and bail (not forgetting to release buffer) } else { [buffer setLength: size]; } imgResultado.image = [UIImage imageWithData: buffer]; [buffer release]; [data release]; data = nil; } else { NSLog(@"No data."); } } break; }} 

它不工作…你能帮我吗?

那么,我是这样做的,但我不确定是否有更好的方法。

 - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode { switch(eventCode) { case NSStreamEventHasBytesAvailable: { uint32_t max_size = 1000000; // Max size of the received imaged. NSMutableData* buffer = [[NSMutableData alloc] initWithLength: max_size]; NSInteger totalBytesRead = 0; NSInteger bytesRead = [(NSInputStream *)stream read: [buffer mutableBytes] maxLength: max_size]; if (bytesRead != 0) { while (bytesRead > 0 && totalBytesRead + bytesRead < max_size) { totalBytesRead+= bytesRead; bytesRead = [(NSInputStream *)stream read: [buffer mutableBytes] + totalBytesRead maxLength: max_size - totalBytesRead]; } if (bytesRead >= 0) { totalBytesRead += bytesRead; } else { // read failure, report error and bail (not forgetting to release buffer) } [buffer setLength: totalBytesRead]; imgResultado.image = [UIImage imageWithData: buffer]; [buffer release]; } } break; }} 

使用这种方法,接收的图像必须小于max_size。 如果你知道更好的方法来做到这一点,请让我知道! ;)

如果你想要做的只是从服务器下载一个图像来显示,看看这个:

https://github.com/michaelkamprath/iPhoneMK/tree/master/Views/MKNetworkImageView

iOS中有很多内置的函数可以为你处理图像下载。 也就是说,如果您可以将要下载的实体封装为单独的URL(例如,Web服务器)。

但是,如果您尝试通过相同的TCP连接发送和接收数据,我强烈build议您更改为更加REST风格的方法。 部分原因是因为从客户端实现起来更容易,部分原因是您不必担心(尽可能多)底层的TCP连接。