遇到这个问题,我想通过凹凸发送图像,但是凹凸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个小时,但无法解决。

最佳答案

好的,我实际上还没有尝试过,但是可以了。

在接收端,将接收到的NSData对象合并到带有-[NSMutableData appendData:]-[NSMutableData appendBytes:length:]的NSMutableData实例中。 (从-[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];
    }
}


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

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


做完了!

关于iphone - NSKeyedUnarchiver如何从凹凸中提取多个块?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10161128/

10-09 18:17