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;

}

}

 

posted @ 2015-09-14 22:06  竹韵潇潇  阅读(433)  评论(0编辑  收藏  举报