将 NSData 分成三个块 - 以我开始的更多字节结束
Split NSData into three chunks - Ending up with MORE bytes that I started with
我有一个具有视频上传功能的 iOS 应用程序。我需要将视频数据上传到具有非常特定上传要求的服务器:最大文件大小 15 MB,并且文件上传必须通过不超过 5 MB 的块上传。
我正在使用 subdataWithRange
将视频文件分成三个 NSData
块。这样,无论文件总大小如何,文件块都不会超过 5 MB。我将 NSData 对象分成三个块并检查了大小,结果发现我以开始时的 MORE 字节结束。我知道我是个特别笨的人。
这是我的代码:
// Get the video data object.
NSData *postData = [self getAttachmentData];
// Get the file size in bytes.
NSUInteger fileSizeBytes = [postData length];
// Split the video into three chunks.
NSData *chunkOne = [postData subdataWithRange:NSMakeRange(0, (fileSizeBytes / 3))];
NSData *chunkTwo = [postData subdataWithRange:NSMakeRange([chunkOne length], ((fileSizeBytes / 3) * 2))];
NSData *chunkThree = [postData subdataWithRange:NSMakeRange([chunkTwo length], (fileSizeBytes - [chunkTwo length]))];
NSLog(@"fileSizeBytes: %lu", (unsigned long)fileSizeBytes);
NSLog(@"\n\n");
NSLog(@"chunckOne: %lu", (unsigned long)[chunkOne length]);
NSLog(@"chunckTwo: %lu", (unsigned long)[chunkTwo length]);
NSLog(@"chunckThree: %lu", (unsigned long)[chunkThree length]);
NSLog(@"\n\n");
NSLog(@"chunkTotal: %lu", ((unsigned long)[chunkOne length] + (unsigned long)[chunkTwo length] + (unsigned long)[chunkThree length]));
这是输出日志:
fileSizeBytes: 2132995
chunckOne: 710998
chunckTwo: 1421996
chunckThree: 710999
chunkTotal: 2843993
那我做错了什么?我相信我已经正确设置了范围,因此对象被分为三个块。
范围是(开始,长度)而不是(开始,结束)。你做:
(0, 710998), (710998, 1421996), (1421996, 710999)。它应该是:(0, 710998), (710998, 710998) 和 (1421996, 710999)
NSUInteger chunkSizeBytes = (fileSizeBytes / 3)
NSData *chunkOne = [postData subdataWithRange:NSMakeRange(0, chunkSizeBytes)];
NSData *chunkTwo = [postData subdataWithRange:NSMakeRange(chunkSizeBytes, chunkSizeBytes)];
NSData *chunkThree = [postData subdataWithRange:NSMakeRange(chunkSizeBytes * 2, fileSizeBytes - (chunkSizeBytes * 2))];
我有一个具有视频上传功能的 iOS 应用程序。我需要将视频数据上传到具有非常特定上传要求的服务器:最大文件大小 15 MB,并且文件上传必须通过不超过 5 MB 的块上传。
我正在使用 subdataWithRange
将视频文件分成三个 NSData
块。这样,无论文件总大小如何,文件块都不会超过 5 MB。我将 NSData 对象分成三个块并检查了大小,结果发现我以开始时的 MORE 字节结束。我知道我是个特别笨的人。
这是我的代码:
// Get the video data object.
NSData *postData = [self getAttachmentData];
// Get the file size in bytes.
NSUInteger fileSizeBytes = [postData length];
// Split the video into three chunks.
NSData *chunkOne = [postData subdataWithRange:NSMakeRange(0, (fileSizeBytes / 3))];
NSData *chunkTwo = [postData subdataWithRange:NSMakeRange([chunkOne length], ((fileSizeBytes / 3) * 2))];
NSData *chunkThree = [postData subdataWithRange:NSMakeRange([chunkTwo length], (fileSizeBytes - [chunkTwo length]))];
NSLog(@"fileSizeBytes: %lu", (unsigned long)fileSizeBytes);
NSLog(@"\n\n");
NSLog(@"chunckOne: %lu", (unsigned long)[chunkOne length]);
NSLog(@"chunckTwo: %lu", (unsigned long)[chunkTwo length]);
NSLog(@"chunckThree: %lu", (unsigned long)[chunkThree length]);
NSLog(@"\n\n");
NSLog(@"chunkTotal: %lu", ((unsigned long)[chunkOne length] + (unsigned long)[chunkTwo length] + (unsigned long)[chunkThree length]));
这是输出日志:
fileSizeBytes: 2132995
chunckOne: 710998
chunckTwo: 1421996
chunckThree: 710999
chunkTotal: 2843993
那我做错了什么?我相信我已经正确设置了范围,因此对象被分为三个块。
范围是(开始,长度)而不是(开始,结束)。你做: (0, 710998), (710998, 1421996), (1421996, 710999)。它应该是:(0, 710998), (710998, 710998) 和 (1421996, 710999)
NSUInteger chunkSizeBytes = (fileSizeBytes / 3)
NSData *chunkOne = [postData subdataWithRange:NSMakeRange(0, chunkSizeBytes)];
NSData *chunkTwo = [postData subdataWithRange:NSMakeRange(chunkSizeBytes, chunkSizeBytes)];
NSData *chunkThree = [postData subdataWithRange:NSMakeRange(chunkSizeBytes * 2, fileSizeBytes - (chunkSizeBytes * 2))];