2
我想计算文件上传到Dropbox的速度。从上传百分比获取上传速度
下面是我目前使用的代码:
int numberOfCalls = 0;
- (float)getUploadSpeed:(float)currentAmountUploaded {
numberOfCalls++;
NSLog(@"called %d times.", numberOfCalls);
NSLog(@"speed approx. %fKB/sec.", (currentAmountUploaded/numberOfCalls)*1024);
return (currentAmountUploaded/numberOfCalls)*1024;
}
- (void)startUploadSpeedTest:(NSNumber *)uploadCurrent {
float uploadedFileSize = [uploadCurrent floatValue];
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:@selector(getUploadSpeed:)];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:@selector(getUploadSpeed:)];
[invocation setTarget:self];
[invocation setArgument:&uploadedFileSize atIndex:2];
[invocation invoke];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[[NSTimer timerWithTimeInterval:1.0 invocation:invocation repeats:YES] retain];
[runLoop run];
[pool release];
}
- (void)restClient:(DBRestClient *)client uploadProgress:(CGFloat)progress forFile:(NSString *)destPath from:(NSString *)srcPath {
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:srcPath error:nil];
float fileSize = ((float)[fileAttributes fileSize]/1024/1024);
float uploadedFileSize = fileSize * progress;
NSNumber *number = [[[NSNumber alloc] initWithFloat:uploadedFileSize] autorelease];
NSThread *timerThread = [[[NSThread alloc] initWithTarget:self selector:@selector(startUploadSpeedTest:) object:number] autorelease];
[timerThread start];
}
但是,计时器不叫每一秒,我不知道这是获得速度的最佳方式。
任何帮助表示赞赏。
非常感谢!无法想出一个方法来使其工作! – 2011-01-29 12:04:12