0

遇到这个问题我想通过bump发送图像,但是bump api的最大块是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];
    } 

我如何将所有块编译在一起?

花了3个小时,但无法解决。

4

1 回答 1

1

好的,我实际上还没有尝试过,但是这里有。

在接收端,获取接收到的 NSData 对象,并将它们合并为 NSMutableData 的实例,使用-[NSMutableData appendData:]or -[NSMutableData appendBytes:length:]-[NSData bytes](从和获取字节-[NSData length]

然后,使用+[NSKeyedUnarchiver unarchiveObjectWithData:].

...像这样(设置了一个 NSMutableData 属性,假设为receivedImageData)...

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

...然后,当您获得最后一个块(它的大小不等于 262144?在-bumpSessionEnded:?)时,将图像对象返回

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

完毕!

于 2012-04-15T10:44:43.823 回答