2012-02-25 30 views
4

iPhoneでクライアントをプログラミングしています。私はいくつかの文字列を送信し、サーバーからイメージを受信したい。iPhone(TCPクライアント)で画像を受信

私はこのチュートリアル(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; 
}} 

それは動作しません...あなたは私を助けることができますか?

答えて

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より小さくなければなりません。 これを行うより良い方法がわかっている場合は、教えてください。 ;)

+0

これは、以前のコードが予期していたように、サーバーがストリームの先頭にサイズの整数を送信していないことを意味します。画像データの場合、最初の4バイトを効果的に削除したため、前のコードが機能しませんでした。 – kamprath

+0

このコードでは、1000000バイトまでの画像を受信できます。それはほぼ1MBですか? (あなたの元のコードは約100kしか持っていませんでしたか?)ただ確認してみてください... – user523234

+1

user523234はい、クライアントは最大1000000バイトの画像を受信できます。 Claireware、それは良い説明かもしれない、私はあなたが正しいと思う。その方法は私の答えよりも安全ですが、私がそれを使いたい場合は、サーバーを変更する必要があります。 – Xithias

0

あなたがしたいすべてが、このを見て、表示するために、サーバからイメージをダウンロードしている場合:

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

に建てられた機能の多くは、画像を処理するのiOSにあります。あなたのためにダウンロードしてください。つまり、ダウンロードするエンティティを別のURL(たとえば、Webサーバー)としてカプセル化できる場合です。

しかし、同じTCP接続を介してデータを送受信しようとしている場合は、よりRESTfulな方法に変更することを強くお勧めします。部分的には、クライアント側から実装する方が簡単であることもあります。これは、基本的なTCP接続について(それほど心配する必要がないため)部分的にです。

+0

イメージはウェブ上にありません。サーバーは、クライアントから送信されたデータを使用してイメージを生成し、TCP接続を介してイメージを送信します。その画像を受信して​​表示するには、iPhoneのTCPクライアントをプログラムする必要があります。 – Xithias

+0

サーバーが返すデータ形式を知っていますか? imageWithData:JPG、PNGなどでのみ動作します。サーバが生のピクセルデータを返す場合、それは動作しません。 – kamprath

+0

受信した画像はJPGです。 – Xithias

関連する問題