【问题标题】:Copy file from one place to another using progress bar in Objective-c [duplicate]使用Objective-c中的进度条将文件从一个地方复制到另一个地方[重复]
【发布时间】:2013-05-05 18:37:46
【问题描述】:

当我将文件从一个地方复制到另一个地方时,我想使用进度条来显示进度。所以这里是复制文件的代码:

 if([fileManager isReadableFileAtPath:sourcePath])
    [fileManager copyItemAtPath:sourcePath toPath:destinationPath error:nil];

这是进度条的代码(我使用 NSProgressIndicator):

// Set the max value to our source file size
[_localProgressIndicator setMaxValue:fileSizeTotal];
[_localProgressIndicator setDoubleValue:0.0];

//Start showing progress bar in using NSTime
if(!timerForProgressBar){
    timerForProgressBar = [NSTimer scheduledTimerWithTimeInterval:0.05
                                                            target:self
                                                         selector:@selector(checkCopyingPorgress:)
                                                          userInfo:nil
                                                           repeats:YES];
    [_localProgressIndicator startAnimation:self];
}

-(void)checkCopyingPorgress:(NSTimer *)aTimer
{
  if(fileCurrentSize >= fileSizeTotal)
  {
     fileCurrentSize = 0;
     [aTimer invalidate];
     aTimer = NULL;
     [_localProgressIndicator setDoubleValue:0.0];
     [_localProgressIndicator stopAnimation: self];
  }
  else
  {
    [_localProgressIndicator setDoubleValue: fileCurrentSize/100.0];
    [_localProgressIndicator displayIfNeeded];
  }
}

所以我的问题是,当我将文件从一个地方复制到另一个地方时,如何从 fileManager 获取“fileCurrentSize”?谢谢 !!

【问题讨论】:

    标签: objective-c macos cocoa nsprogressindicator


    【解决方案1】:

    你可以使用

    - (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error
    

    然后从文件属性字典中获取[dictObject valueForKey@"NSFileSize"];

    【讨论】:

    • 如何获取?当我调用它时,它只会给我整个文件大小.. 而不是当前的。
    • 你的意思是你想知道新文件被复制了多少。
    • 那种。当我调用 NSFileSize 函数时,它只是给了我文件的整个大小。复制时是否可以获取当前大小,以便我可以使用进度条。
    • 你可以试试“- (unsigned long long)fileSize;”代替方法。 IIRC 这将返回您所期望的,而“NSFileBusy”属性为 YES。
    猜你喜欢
    • 2013-07-12
    • 2021-04-06
    • 1970-01-01
    • 2014-07-29
    • 2022-01-21
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多