【发布时间】:2016-06-15 21:58:27
【问题描述】:
我正在尝试通过套接字将 UIImage 从 iOS 发送到 Unity。我将 UIImage 转换为 NSData 并通过异步套接字将其发送到 Unity 中的服务器。
我正在尝试从客户端(iOS 应用程序)接收所有字节并将这些字节转换为图像并将其作为图像保存在计算机中。我一直收到文件错误,要么是空的,要么是损坏的。
不知道是iOS端、Unity端还是两者都有错误。
在我的 iOS 端,我有:
UIImage *img = images[number];
NSData *dataImg = UIImagePNGRepresentation(img);
[asyncSocket writeData:dataImg withTimeout:-1 tag:1]; // Uses GCDA Async Socket library
[asyncSocket disconnectAfterWriting];
在 Unity 方面,我有:
// 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());
}
}
【问题讨论】:
标签: c# ios arrays sockets unity3d