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;

    }

    }

  • 相关阅读:
    C#中Dictionary<TKey,TValue>排序方式
    反射之取类中类的属性、变量名称及其值
    程序测试用的IE浏览器第二次无法加载入口程序的问题及其解决方法
    使用Windows Form 制作一个简易资源管理器
    如何查看自制词典的执行效率
    cocos2dx 3.12 eclipse编辑器切换到Android Studio
    Cordova安装使用
    Activity的启动模式
    踩坑集锦——MVC权限验证
    设计模式学习之路——策略模式
  • 原文地址:https://www.cnblogs.com/xiangruru/p/4808338.html
Copyright © 2011-2022 走看看