views:

166

answers:

2

Hey all!

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!

+1  A: 

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.

drawnonward
Ah, dang I was hoping not to have to do that.How to I make sure I get the integer first?Can I assume that things are being dumped as int,image,int,image,etc?So I read the first few bytes, then read the amount of bytes specified, then read the next few bytes, then read the amount of bytes specified?Could you provide an example or layout? I don't need specific code.
Jus' Wondrin'
If you are using a tcp socket, then yes, you can assume your lengths will come in before the image. You will then read the first 4 bytes (for your int) then read the value of those 4 bytes into your data buffer.
Elfred
A: 

Just use a delimiter to separately Identify the different Images. When you catch the delimiter you know the other image comes next.

charith