zoukankan      html  css  js  c++  java
  • ios优化复制大文件时,如何使内存运用最少且效率最高

    我也是纠结了好几天,我想自己想个办法,但是数据复制不上去,我现在还不明白,如果有人知道我错在哪了,请留言,如果还有更好的方法,请分享共同进步。

    ________________________________________用输入流进行文件的复制

    //这是我原图片的路径

            NSString *Path=[NSHomeDirectory() stringByAppendingPathComponent:@"2238-110H514215143.jpg"];

            NSError *error;

    //这是我想要复制图片的路径

            NSString *Path1=[NSHomeDirectory() stringByAppendingPathComponent:@"Movies/2238-110H514215143oooo.jpg"];

            NSLog(@"%@",Path1);

    //创建一个文件对象

            NSFileManager *fileManager=[NSFileManager defaultManager];

    //创建一个空的准备写入数据的文件

            BOOL success=[fileManager createFileAtPath:Path1 contents:nil attributes:nil];

            if (success) {

                NSLog(@"复制文件成功");

            }

    //获取复制文件的属性全部

            NSDictionary *dictionary=[fileManager attributesOfItemAtPath:Path error:&error];

    //获取文件的大小

            NSNumber *number=[dictionary objectForKey: NSFileSize];

            long long a=[number longLongValue];

    //为读和写作准备

            NSFileHandle *fileHandle=[NSFileHandle fileHandleForReadingAtPath:Path];

            NSFileHandle *filehandle=[NSFileHandle fileHandleForWritingAtPath:Path1];

            NSMutableData *data1;

            //如何实现读取5000字节写入5000字节

    //        for (long long l=0; l<=a; ) {

    //        for (int i=0; i<=5000;i++) {

    //        NSData *data2=[fileHandle readDataOfLength:l];     //这是我想的但是行不通不知是为什么

    //        [data1 appendData:data2];

    //            l++;

    //        }

    //         [filehandle writeData:data1];

    //        }

            while(a) {

                NSData *data;

                if (a<5000){

    //如果文件小于5000字节是直接复制

                  data=[fileHandle readDataToEndOfFile];

                  [filehandle writeData:data];

                }else{

    //这是从头到尾读取5000字节

                    data=[fileHandle readDataOfLength:5000];

                    [filehandle writeData:data];

    //a减是从头减5000字节正好是剩下的当a为0时正好是否

                    a-=5000;

                }

            }

            [fileHandle closeFile];

            [filehandle closeFile];

        return 0;

    }

    }

  • 相关阅读:
    Linux基础3-1 Bash及其特性
    三、手写ORM实现数据库更新
    三、TCP协议
    一、OIS七层模型及数据传输过程
    泛型缓存原理
    树莓派公网服务器实现frp内网穿透
    Dto数据传输对象
    Ubuntu下 Nginx静态代理部署网页常见报错
    JWT权限验证
    解决传入的请求具有过多的参数,该服务器支持最多 2100 个参数
  • 原文地址:https://www.cnblogs.com/xiangruru/p/4808338.html
Copyright © 2011-2022 走看看