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

时间:2012-02-25 20:32:22

标签: iphone ios image tcp client-server

我正在iPhone上编写客户端。我想发送一些字符串,并从服务器接收图像。

我找到了这个教程(http://www.devx.com/wireless/Article/43551),它一直非常有用。如果我想接收字符串,它可以工作,但现在我想要接收图像,但我无法使其工作。

当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;
}}

它不起作用......你能帮助我吗?

2 个答案:

答案 0 :(得分:3)

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

- (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。 如果您知道更好的方法,请告诉我! ;)

答案 1 :(得分:0)

如果您只想从要显示的服务器下载图像,请查看以下内容:

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

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

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