国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

KVO實現自定義文件復制進度效果

2019-10-21 18:45:05
字體:
來源:轉載
供稿:網友

本文實例為大家分享了KVO實現自定義文件復制進度展示的具體代碼,供大家參考,具體內容如下

一、創建文件

說明:自定義文件類,通過NSFileManager 以及NSFileHandle 實現文件的創建和copy,為了控制內存的并發使用,通過控制每次賦值的固定長度來分多次復制:

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,并提供方法接口,啟動文件拷貝。

- (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];}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临朐县| 辽阳县| 临安市| 漳平市| 新闻| 滨海县| 新闻| 全州县| 会昌县| 张家口市| 平凉市| 景东| 峡江县| 万宁市| 定襄县| 桂平市| 察隅县| 芮城县| 高淳县| 镇安县| 尉氏县| 中方县| 章丘市| 宜春市| 朝阳区| 大新县| 万年县| 嘉峪关市| 桂平市| 石家庄市| 贵定县| 杭州市| 乌兰浩特市| 防城港市| 长治市| 闸北区| 辽源市| 淮北市| 淄博市| 巍山| 安乡县|