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;

}

}

 

转载于:https://www.cnblogs.com/xiangruru/p/4808338.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值