iphone - 如何确定调用 moveItemAtURL :toURL: or replaceItemAtURL:WithItemAtURL: 时需要多少时间

标签 iphone objective-c xcode cocoa nsfilemanager

当将文件从一个地方移动到另一个地方或替换文件时,我总是使用方法 moveItemAtURL:toURL:replaceItemAtURL:WithItemAtURL:来自NSFileManager .

当调用这些方法时,我想确定需要多少时间,以便我可以使用NSProgressIndicator告诉用户需要多长时间。就像您使用 OSX 移动文件一样,它会告诉您剩余时间。

我查看了苹果文档,但找不到与此相关的任何信息。 想知道是否可以实现,请指教。

最佳答案

你无法提前知道需要多长时间。您可以做的是在复制文件时计算“完成百分比”。但要做到这一点,您需要使用较低级别的 API。您可以使用 NSFileManagers attributesOfItemAtPath:error 来获取文件大小和 NSStreams 来进行复制(有很多方法可以做到这一点)。完成百分比为bytesWritten/totalBytesInFile

--- 编辑:在 NSURL 上添加示例代码作为类别,并使用回调 block 传递写入的总字节数、完成百分比和估计剩余时间(以秒为单位)。

#import <mach/mach_time.h>

@interface NSURL(CopyWithProgress)<NSObject>
- (void) copyFileURLToURL:(NSURL*)destURL withProgressBlock:(void(^)(double, double, double))block;
@end

@implementation NSURL(CopyWithProgress)

- (void) copyFileURLToURL:(NSURL*)destURL
        withProgressBlock:(void(^)(double, double, double))block
{
    ///
    // NOTE: error handling has been left out in favor of simplicity
    //       real production code should obviously handle errors.
    NSUInteger fileSize = [[NSFileManager defaultManager] attributesOfItemAtPath:self.path error:nil].fileSize;

    NSInputStream  *fileInput  = [NSInputStream inputStreamWithURL:self];
    NSOutputStream *copyOutput = [NSOutputStream outputStreamWithURL:destURL append:NO];

    static size_t bufferSize = 4096;
    uint8_t *buffer = malloc(bufferSize);
    size_t   bytesToWrite;
    size_t   bytesWritten;
    size_t   copySize = 0;
    size_t   counter  = 0;

    [fileInput open];
    [copyOutput open];

    uint64_t time0 = mach_absolute_time();

    while (fileInput.hasBytesAvailable) {
        do {
            bytesToWrite = [fileInput read:buffer maxLength:bufferSize];
            bytesWritten = [copyOutput write:buffer maxLength:bytesToWrite];
            bytesToWrite -= bytesWritten;
            copySize     += bytesWritten;
            if (bytesToWrite > 0)
                memmove(buffer, buffer + bytesWritten, bytesToWrite);
        }
        while (bytesToWrite > 0);

        if (block != nil && ++counter % 10 == 0) {
            double percent  = (double)copySize / fileSize;
            uint64_t time1  = mach_absolute_time();
            double elapsed  = (double)(time1 - time0)/NSEC_PER_SEC;
            double estTimeLeft = ((1 - percent) / percent) * elapsed;
            block(copySize, percent, estTimeLeft);
        }
    }

    if (block != nil)
        block(copySize, 1, 0);
}

@end


int main (int argc, const char * argv[])
{
    @autoreleasepool {

        NSURL *fileURL = [NSURL URLWithString:@"file:///Users/eric/bin/data/english-words.txt"];
        NSURL *destURL = [NSURL URLWithString:@"file:///Users/eric/Desktop/english-words.txt"];

        [fileURL copyFileURLToURL:destURL withProgressBlock:^(double bytes, double pct, double estSecs) {
            NSLog(@"Bytes=%f, Pct=%f, time left:%f s",bytes,pct,estSecs);
        }];
    }
    return 0;
}

示例输出:

Bytes=40960.000000, Pct=0.183890, time left:0.000753 s
Bytes=81920.000000, Pct=0.367780, time left:0.004336 s
Bytes=122880.000000, Pct=0.551670, time left:0.002672 s
Bytes=163840.000000, Pct=0.735560, time left:0.001396 s
Bytes=204800.000000, Pct=0.919449, time left:0.000391 s
Bytes=222742.000000, Pct=1.000000, time left:0.000000 s

关于iphone - 如何确定调用 moveItemAtURL :toURL: or replaceItemAtURL:WithItemAtURL: 时需要多少时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16364136/

相关文章:

iphone - 基于 PhoneGap 的 iOS 应用程序缺少基本 SDK

iphone - 我应该在 OpenGL ES 游戏中使用多线程吗?

iphone - 在 Objective-C 中编程 Stephen Kochan。第 21 章,练习 21.2

ios - UITabBarController 选中项显示导航栏

iphone - APNS 托管提供商

iphone - 不必要的失真:核心图像滤镜正在旋转我的图像

iphone - 同步访问 UITableViewDelegate 逻辑

iphone - 在 View 中设置 searchBar.text 确实加载了

swift - Storyboard约束不影响自定义 UiView 大小

ios - 固定顶部的 UIScrollview 弹跳和缩放