我正在将视频从URL下载到文档目录。视频没有固定的大小。如何显示用户的下载状态。有没有办法显示要下载的剩余字节?
这是我的代码 -
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *urlToDownload = @"http://someurl.mp4";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp4", documentsDirectory,[[[array objectAtIndex:i] objectForKey:@"name"]stringByReplacingOccurrencesOfString:@" " withString:@"_"]];
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(@"File Saved -------- %@!",filePath);
});
}
}
});
答案 0 :(得分:0)
改为使用NSURLConnection:http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/
并使用其委托函数来取得进展:https://stackoverflow.com/a/2269670/78496