美文网首页
基础算法对比

基础算法对比

作者: 愤怒小鸟飞呀飞 | 来源:发表于2018-05-03 11:28 被阅读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];    
            }
        }
    

    代码需要优化,需要思考

    相关文章

      网友评论

          本文标题:基础算法对比

          本文链接:https://www.haomeiwen.com/subject/vpskrftx.html