在iPhone上接收图像(TCP客户端) [英] Receive an image on the iPhone (TCP Client)

查看:194
本文介绍了在iPhone上接收图像(TCP客户端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPhone上编写一个客户端。

I'm programming a client on the iPhone. I want to send some strings, and receive an image from the server.

我发现这个教程(http://www.devx.com/wireless/Article/43551) ),它已经非常有用。

I found this tutorial (http://www.devx.com/wireless/Article/43551), it has been very useful. It works if I want to receive strings, but now I want to receive an image, and I can't make it work.

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

This is my code when the iPhone receives data. It's a mix made with the tutorial and this answer from 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;
}}

它不工作...你能帮助我吗? p>

It doesn't work... could you help me?

推荐答案

好吧,我使用这种方式工作,但我不知道是否有更好的方法。

Well, I made it work this way, but I'm no sure if there's a better way.

- (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。
如果你知道更好的方法来做到这一点,请让我知道! ;)

With this method, the received image must be smaller than max_size. If you know a better way to do this, please let me know! ;)

这篇关于在iPhone上接收图像(TCP客户端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆