如何从套接字中从 NSData 中提取 UIImages
我正在使用 ASyncSocket 将一些 UIImages 从一台设备移动到另一台设备。
本质上,在我拥有的一台设备上:
NSMutableData *data = UIImageJPEGRepresentation(image, 0.1);
if(isRunning){
[sock writeData:data withTimeout:-1 tag:0];
}
因此,每隔一段时间就会向套接字添加一个新图像(如网络摄像头)。
然后,在另一台设备上,我调用:
[listenSocket readDataWithTimeout:1 tag:0];
它将响应:
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
[responseData appendData:data];
[listenSocket readDataWithTimeout:1 tag:0];
}
本质上,我想要做的是让 NSTimer 运行,它将调用 @selector(PullImages):
-(void) PullImages {
在这里,我希望能够拉来自 ResponseData 的图像。我该怎么做? 可能还没有完整的图像,可能有多个图像,可能有一张半图像!
我想将 NSData 解析为每个现有图像!
}
有什么帮助吗?提前致谢!
I'm using ASyncSocket to move some UIImages from one device over to another.
Essentially, on one device I have:
NSMutableData *data = UIImageJPEGRepresentation(image, 0.1);
if(isRunning){
[sock writeData:data withTimeout:-1 tag:0];
}
So a new image will be added to the socket every so often (like a webcam).
Then, on the other device, I am calling:
[listenSocket readDataWithTimeout:1 tag:0];
which will respond with:
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
[responseData appendData:data];
[listenSocket readDataWithTimeout:1 tag:0];
}
Essentially, what I want to be able to do is have an NSTimer going which will call @selector(PullImages):
-(void) PullImages {
In here, I want to be able to pull images out of ResponseData. How do I do that?
There might not be a complete image yet, there might be multiple images, there might be one and a half images!
I want to parse the NSData into each existing image!
}
Any assistance? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须围绕图像实现您自己的包装协议。这可以像在实际图像数据之前发送图像数据的长度一样简单。接收数据时,您现在知道在获得完整图像之前需要提取多少字节。当每个图像从流中分离出来时,您就有了下一个图像的长度。
由于您是从一台 iPhone 发送到另一台 iPhone,因此不必担心字节顺序,只需在每一端使用 32 位 int 来表示大小。
You have to implement your own wrapper protocol around the images. That can be as simple as sending the length of the image data before the actual image data. When receiving data, you now know how many bytes you need to pull before you have a complete image. As each image is split out of the stream, you have the length of the next image.
Since you are sending from one iPhone to another, you do not have to worry about endianess and can just use a 32 bit int at each end for the size.
只需使用分隔符来单独识别不同的图像即可。当您捕获分隔符时,您知道接下来会出现另一个图像。
Just use a delimiter to separately Identify the different Images. When you catch the delimiter you know the other image comes next.