如何显示在复制的iOS一个大文件的进展如何?(How to show the progress of

2019-06-24 03:22发布

我写的iOS应用。 在我的应用程序,我想复制一些文件从一个文件夹到另一个。 但是,因为一些文件过大,将需要很长的时间来完成复制。 所以,我想添加一个进度条,显示复制的百分比。 但我发现,文件管理器没有回调方法得到的百分比。 有没有人有很好的解决方案呢?

Answer 1:

在高级别:

  1. 运行在一个单独的线程(T1)的复制过程
  2. 运行另一个线程(T2),其中定期读取(比如每100ms)目标文件current_size
  3. 计算百分比: current_size / total_size
  4. 更新你的进步吧UI元素


Answer 2:

我已经创建了@giorashc方法的简单类。

如果有人需要的东西像这样随意使用它。

该.H

#import <UIKit/UIKit.h>

@protocol IDCopyUtilsDelegate;

@interface IDCopyUtils : NSObject

@property (nonatomic, weak) id<IDCopyUtilsDelegate> delegate;

- (void)copyFileAtPath:(NSString *)sourcePath toPath:(NSString *)targetPath;

@end

// 3. Definition of the delegate's interface
@protocol IDCopyUtilsDelegate <NSObject>

- (void)setCopyProgress:(float)progress;
- (void)didFinishedCopyWithError:(NSError *)error;

@end

该.M

#import "IDCopyUtils.h"

@interface IDCopyUtils()

@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) NSString *sourcePath;
@property (nonatomic, strong) NSString *targetPath;

@end

@implementation IDCopyUtils

- (void)copyFileAtPath:(NSString *)sourcePath toPath:(NSString *)targetPath
{
    self.sourcePath = sourcePath;
    self.targetPath = targetPath;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    if ([fileManager fileExistsAtPath:self.targetPath] == YES) {
        [fileManager removeItemAtPath:self.targetPath error:&error];
    }

    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.100
                                     target:self
                                   selector:@selector(checkFileSize)
                                   userInfo:nil
                                    repeats:YES];

    [self performSelector:@selector(startCopy) withObject:nil afterDelay:0.5];

}

- (void)checkFileSize
{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSDictionary *attributesSource = [[NSFileManager defaultManager] attributesOfItemAtPath:self.sourcePath error:NULL]; unsigned long long fileSize = [attributesSource fileSize];

        NSDictionary *attributesTarget = [[NSFileManager defaultManager] attributesOfItemAtPath:self.targetPath error:NULL]; unsigned long long fileSizeTarget = [attributesTarget fileSize];

        double progress = (float)fileSizeTarget / (float)fileSize;

        if (self.delegate && [self.delegate respondsToSelector:@selector(setCopyProgress:)])
        {
            [self.delegate setCopyProgress:progress];
        }

        NSLog(@"Size: %f", progress);
    });
}

- (void)startCopy
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;

        if ([fileManager fileExistsAtPath:self.targetPath] == YES) {
            [fileManager removeItemAtPath:self.targetPath error:&error];
        }

        if ([fileManager fileExistsAtPath:self.targetPath] == NO) {
            [fileManager copyItemAtPath:self.sourcePath toPath:self.targetPath error:&error];

            [self.timer invalidate];
            self.timer = nil;

            if (self.delegate && [self.delegate respondsToSelector:@selector(didFinishedCopyWithError:)])
            {
                [self.delegate didFinishedCopyWithError:error];
            }
        }
    });
}

@end

您可以使用它像这样(例如):

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"iso"];
NSString *targetPath = [documentsDirectory stringByAppendingPathComponent:@"test.iso"];

IDCopyUtils *copyUtils = [[IDCopyUtils alloc] init];
copyUtils.delegate = self;
[copyUtils copyFileAtPath:sourcePath toPath:targetPath];

你会我们可以更新你的进步观点和得到通知时,文件并使用委托方法finidhed复制。



文章来源: How to show the progress of copying a large file in iOS?