KVO实现自定义文件复制进度展示
2015-04-22 15:29
211 查看
一、创建文件 |
NSString * path=NSHomeDirectory(); path =[path stringByAppendingPathComponent:@"deskTop/Boby.m"]; NSString * target=NSHomeDirectory(); target =[target stringByAppendingPathComponent:@"deskTop/target.m"]; NSFileManager * manager=[NSFileManager defaultManager]; //校验并且创建文件 if(![manager fileExistsAtPath:path]){ [manager createFileAtPath:path contents:nil attributes:nil]; } if(![manager fileExistsAtPath:target]){ [manager createFileAtPath:target contents:nil attributes:nil]; } NSDictionary * dic=[manager attributesOfItemAtPath:path error:nil]; NSFileHandle * handle=[NSFileHandle fileHandleForReadingAtPath:path]; NSFileHandle * handletTarget=[NSFileHandle fileHandleForWritingAtPath:target]; int total=(int)[dic[@"NSFileSize"] integerValue]; self.totalSize=total; int per=50; int count=total%per==0?total/per:total/per+1; for(int i=0;i<count;i++){ [handle seekToFileOffset:self.nowSize]; NSData *data= [handle readDataOfLength:per]; int tem=per*(i+1); if(tem>total){ tem=total; } self.nowSize=tem; [handletTarget seekToEndOfFile]; [handletTarget writeData:data]; [NSThread sleepForTimeInterval:0.2]; } [handle closeFile]; [handletTarget closeFile];
二、设置观察者 |
- (id) initWithFile:(FileMake *)files{ self=[super init]; if(self){ self.file= files; [self.file addObserver:self forKeyPath:@"nowSize" options:NSKeyValueObservingOptionNew context:nil]; } return self; } -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ CGFloat all=self.file.totalSize; CGFloat now=[[change objectForKey:@"new"] floatValue]; CGFloat result=now/all; NSLog(@"%.2f",result); //一定不能忘了销毁当前的观察者 if(result==1){ [self.file removeObserver:self forKeyPath:@"nowSize"]; } } - (void) begin{ [self.file startCopy]; }
[/code]
作者:杰瑞教育
出处:http://blog.csdn.net/jerehedu
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
相关文章推荐
- KVO实现自定义文件复制进度展示
- KVO实现自定义文件复制进度展示
- KVO实现自定义文件复制进度效果
- [C#]实现文件复制[更新]实时显示进度条
- android自定义Dialog实现文件下载和下载进度
- android自定义Dialog实现文件下载和下载进度
- android自定义Dialog实现文件下载和下载进度
- IFileOperation实现复制文件时显示进度对话框
- 封装CopyFileEx函数,实现文件复制中的暂停,控速,获取进度
- Android 实现应用下载并在通知栏展示进度通知 打开APK文件
- SpringMVC实现的文件上传进度条Bootstrap展示
- VC实现显示文件复制的进度
- 使用多线程实现文件的复制功能,并在控制台显示复制的进度,进度以百分比表示。
- 简单实现的自定义圆环进度条,可使用于文件的上传下载图片加载等地方.
- IFileOperation实现复制文件时显示进度对话框
- 封装CopyFileEx函数,实现文件复制中的暂停,控速,获取进度。
- 封装CopyFileEx函数,实现文件复制中的暂停,控速,获取进度
- 封装CopyFileEx函数,实现文件复制中的暂停,控速,获取进度。
- Java实现在复制文件时使用进度条
- java实现在复制文件时使用进度条(java实现进度条)