时间:2021-05-02
本文实例为大家分享了KVO实现自定义文件复制进度展示的具体代码,供大家参考,具体内容如下
一、创建文件
说明:自定义文件类,通过NSFileManager 以及NSFileHandle 实现文件的创建和copy,为了控制内存的并发使用,通过控制每次赋值的固定长度来分多次复制:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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];二、设置观察者
说明:自定义使用者,通过设置观察者来动态观察当前文件copy的进度并展示到控制台或者输出到UI,并提供方法接口,启动文件拷贝。
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 - (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]; }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/jerehedu/p/4377162.html
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
1,实现效果2,实现代码:【1】shape_drawable.xml文件【2】我们将该自定义环形圈设置给一个旋转动画,并利用该旋转动画自定义成一个环形进度圈的s
Android实现环形进度条的效果图如下:自定义控件:AttendanceProgressBar代码如下:publicclassAttendanceProgre
本文实例讲述了Android编程实现自定义进度条颜色的方法。分享给大家供大家参考,具体如下:android自定义进度条颜色先看图基于产品经理各种自定义需求,经过
本文介绍WPF一种自定义按钮的方法。实现效果使用图片做按钮背景;自定义鼠标进入时效果;自定义按压效果;自定义禁用效果实现效果如下图所示:实现步骤创建Custom
在Android开发中,往往要用到自定义的控件来实现我们的需求或效果。在使用自定义控件时,难免要用到自定义属性,那怎么使用自定义属性呢?在文件res/value