0

encountered this problem i wanna sent images through bump, but bump api's max chunk is 256kb

 if([[NSKeyedArchiver archivedDataWithRootObject:self.selectedImg]length] > 262144)
    {
        int dlen = [[NSKeyedArchiver
                     archivedDataWithRootObject:self.selectedImg] length];
        NSLog(@"Sending data: %i bytes in %d chunks",dlen,
              (int)ceil(((float)dlen / 262144.0f)));
        for (int i=1; i <= (int)ceil(((float)dlen / 262144.0f)); i++) {
            int maxr=0;
            if ((262144*i) > dlen) {
                maxr = dlen-(262144*(i-1));
            } else {
                maxr = 262144;
            }

            NSData *moveChunk = [[NSKeyedArchiver
                                  archivedDataWithRootObject:self.selectedImg]
                                 subdataWithRange:NSMakeRange(262144*(i-1),maxr)];
            NSLog(@"Sending Chunk: %d, %d bytes",i,[moveChunk length]);
            [bumpObject sendData:moveChunk];
        }
    }
    else
    {
        //Data is 254kb or under
        NSData *moveChunk = [NSKeyedArchiver
                             archivedDataWithRootObject:self.selectedImg];
        [bumpObject sendData:moveChunk];
    } 

how do i compile all the chunk together ?

been spending 3 hours, but can't solve it.

Desmond
  • 5,001
  • 14
  • 56
  • 115
  • 1
    why don't you assign the value of `[NSKeyedArchiver archivedDataWithRootObject:self.selectedImg]` to a variable? I think creating a data object might not be trivial on system resources... (also, why don't you just do `dlen / 262144`? Integer divide will truncate anyway). – samson Apr 15 '12 at 10:22
  • err this is sending data, which is fine. but i need to know how to get back from another side of the device – Desmond Apr 15 '12 at 10:40
  • 1
    it's just hard to read that way. – samson Apr 15 '12 at 10:45

1 Answers1

1

Ok, I haven't actually tried this, but here goes.

On the receiving end, take the received NSData objects, and conglomerate them into an instance of NSMutableData with -[NSMutableData appendData:] or -[NSMutableData appendBytes:length:]. (get the bytes from -[NSData bytes] and -[NSData length]

Then, get the image back using +[NSKeyedUnarchiver unarchiveObjectWithData:].

... like this (have an NSMutableData property set up, let's say called receivedImageData) ...

- (void) bumpDataReceived:(NSData *)chunk {
    if (!self.receivedImageData) {
        self.receivedImageData = [NSMutableData dataWithCapacity:[chunk length]];
        [self.receivedImageData setData:chunk];
    } else {
        [self.receivedImageData appendData:chunk];
    }
}

... and then, when you get the last chunk (it's size isn't equal to 262144? in -bumpSessionEnded:?) get the image object back with

UIImage* receivedImage = [NSKeyedUnarchiver unarchiveObjectWithData:self.receivedImageData];

Done!

samson
  • 1,152
  • 11
  • 23