2016-06-15 1 views
0

소켓을 통해 iOS에서 Unity로 UIImage를 보내려고합니다. UIImage를 NSData로 변환하고 비동기 소켓을 통해 Unity의 서버에 보냅니다.iOS의 Unity C#에서 소켓을 통해 이미지를 바이트 단위로 수신합니다.

클라이언트 (iOS 앱)에서 모든 바이트를 수신하고이 바이트를 이미지로 변환하여 컴퓨터에 이미지로 저장하려고합니다. 파일에 모든 시간 오류가 발생했습니다. 비어 있거나 손상되었습니다.

iOS 쪽, Unity 쪽 또는 둘 다에서 오류가 발생했는지 알 수 없습니다. 유니티 측에서

UIImage *img = images[number]; 
NSData *dataImg = UIImagePNGRepresentation(img); 
[asyncSocket writeData:dataImg withTimeout:-1 tag:1]; // Uses GCDA Async Socket library 
[asyncSocket disconnectAfterWriting]; 

, 내가 가진 : 내 아이폰 OS 측에서

, 내가 가진 빈

// Data buffer for incoming data. 
    byte[] bytes = new Byte[1024]; 

    // host running the application. 
    Debug.Log("Ip " + getIPAddress().ToString()); 
    IPAddress[] ipArray = Dns.GetHostAddresses(getIPAddress()); 
    IPEndPoint localEndPoint = new IPEndPoint(ipArray[0], 1755); 

    // Create a TCP/IP socket. 
    listener = new Socket(ipArray[0].AddressFamily, 
          SocketType.Stream, ProtocolType.Tcp); 

    // Bind the socket to the local endpoint and 
    // listen for incoming connections. 

    try 
    { 
     listener.Bind(localEndPoint); 
     listener.Listen(10); 

     // Start listening for connections. 
     while (true) 
     { 
      Debug.Log("Client Connected"); 

      Socket handler = listener.Accept(); 

      while (true) { 
       bytes = new byte[1024]; 
       int bytesRec = handler.Receive(bytes); 
       data += Encoding.ASCII.GetString(bytes,0,bytesRec); 

       Debug.Log("Bytes rec: " + bytesRec); 
       if (bytesRec <= 0) { 

        // Save image in folder from bytes 
        File.WriteAllBytes(@"images/img.png",bytes); 

        break; 
       } 

      } 

      //File.WriteAllBytes(@"images/prova.png",dataImg); 

      System.Threading.Thread.Sleep(1); 
     } 
    } 
    catch (Exception e) 
    { 
     Debug.Log(e.ToString()); 
    } 
} 

답변

0

를 않거나 손상된

bytes = new byte[1024];

은 아니다 평균 이미지를 저장할 정도로 1024에서 최대 400,000 범프. 또한 bytes = new byte[400000];

해야

, 그것은 당신이 할 경우, 충전되지 않은 다른 바이트는 PNG 파일 그래서이 손상 될 수 있습니다 파일에 기록됩니다 보인다.

는 그 문제를 해결 작동하면 나중에 향상시킬 수

byte []newImage = new byte[bytesRec]; 
Array.Copy(bytes, newImage, bytesRec); 
System.IO.File.WriteAllBytes(@"images/img.png", bytes); 

이 함께

System.IO.File.WriteAllBytes(@"images/img.png", bytes); 

를 교체하십시오.