基础算法对比
2018-05-03 本文已影响0人
愤怒小鸟飞呀飞
上传图片需要切片上传(一张图片分为n个小块上传)每张图片切后的分片数组 组成代码如下
//优秀代码
NSMutableArray *imagePie = [NSMutableArray arrayWithCapacity:5];
NSInteger length = imgData.length;
int i = 0;
while (i * sizeOfImgPie < length) {
NSInteger location = i * sizeOfImgPie;
NSInteger pie_len = MIN(sizeOfImgPie,length - location);
NSData *pie = [imgData subdataWithRange:NSMakeRange(location, pie_len)];
[imagePie addObject:pie];
i++;
}
//自己写的代码
NSInteger p1Count = [p1ImgData length]/kImageUpload_imagePie_size;
NSInteger lastDataLength = [p1ImgData length]%kImageUpload_imagePie_size;
if (lastDataLength) {
p1Count = p1Count + 1;
}
for (int i = 0; i < p1Count; i++) {
int dataLength = kImageUpload_imagePie_size;
if (i == p1Count - 1) {
dataLength = lastDataLength;
}
NSData *pieData = [p1ImgData subdataWithRange:NSMakeRange(i*kImageUpload_imagePie_size, dataLength)];
if (pieData.length) {
[p1ImgPieArr addObject:pieData];
}
}
代码需要优化,需要思考