开发者

How to pull UIImages from NSData from a socket

开发者 https://www.devze.com 2022-12-30 03:27 出处:网络
I\'m using ASyncSocket to move some UIImages from one device over to another. Essentially, on one device I have:

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 wh开发者_开发知识库ich 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号