2012-02-25 3 views
4

iPhone에서 클라이언트를 프로그래밍 중입니다. 일부 문자열을 보내고 서버에서 이미지를 수신하고 싶습니다.iPhone에서 이미지 받기 (TCP 클라이언트)

이 튜토리얼 (http://www.devx.com/wireless/Article/43551)이 매우 유용합니다. 문자열 수신을 원한다면 작동하지만 이미지를 받고 싶습니다. 작동하지 않을 수 있습니다.

iPhone이 데이터를 수신 할 때의 코드입니다.

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

그것은 작동하지 않습니다는 ... 당신이 나를 도울 수 :이 튜토리얼과 JeremyP (http://stackoverflow.com/questions/4613218)에서이 답변으로 만든 혼합입니까? 당신이 원하는 모든 표시 할 서버에서 이미지를 다운로드하는 경우

답변

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 바이트의 이미지를 수신 할 수 있습니다. 클레어웨어는 좋은 설명이 될 수 있습니다. 나는 당신이 옳다고 생각합니다. 그 방법은 내 대답보다 안전하지만 사용하고 싶다면 서버를 수정해야합니다. – Xithias

0

은, 이것 좀보세요 :

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

이미지를 처리 ​​할 아이폰 OS에 기능 내장 많이 있습니다 당신을 위해 다운로드. 즉, 다운로드하려는 엔티티를 별도의 URL로 캡슐화 할 수 있다면 (예 : 웹 서버)

그러나 동일한 TCP 연결을 통해 데이터를 보내고 받으려고한다면 좀 더 RESTful 한 방법으로 변경하는 것이 좋습니다. 부분적으로는 클라이언트 쪽에서 구현하는 것이 더 쉽기 때문에 부분적으로 기본 TCP 연결에 대해 걱정할 필요가 없기 때문입니다.

+0

이미지가 웹에 없습니다. 서버는 클라이언트가 보낸 데이터로 이미지를 생성 한 다음 TCP 연결을 통해 이미지를 보냅니다. 그 이미지를 수신하고 표시하려면 iPhone에서 TCP 클라이언트를 프로그래밍해야합니다. – Xithias

+0

서버가 반환하는 데이터 형식을 알고 있습니까? imageWithData : JPG, PNG 등에서 만 작동합니다. 서버가 원시 픽셀 데이터를 반환하면 작동하지 않습니다. – kamprath

+0

받은 이미지가 JPG입니다. – Xithias