ios - 返回上一个 Controller 并再次返回时如何获取视频的下载状态?

标签 ios objective-c

嗨,我正在下载视频并将其存储到 iPhone 中的文档文件夹中...但是当我单击下载按钮时,视频将开始下载并且进度将很好地显示...但是当我返回到上一个 View Controller 并出现时下载过程中返回下载 Controller ,进度 View 状态将不会显示当前状态...任何人都可以帮我解决这个问题吗?提前致谢...

这是我的代码...

- (void)downLoad:(UIButton *)sender {
_downloadBtn.hidden = YES;
_videoData = [[NSMutableData alloc] init];
_resourceName = [_resourceNameArray objectAtIndex:sender.tag];
_resourceType = [_contentTypesArray objectAtIndex:sender.tag];

NSString *urlStr = [NSString stringWithFormat:@"%@",[_videoUrlArray objectAtIndex:sender.tag]];
[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]] delegate:self ];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Downloading Started");
_length = [response expectedContentLength];
NSLog(@"Size:%0.2f",_length);
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [_videoData appendData:data];
    float progress = (float)[_videoData length]/(float)_length;
    NSLog(@"Progress:%0.2f",progress);
    //_timeLabel.text = [NSString stringWithFormat:@"%0.2F%%",progress*100];
    [_progressView setProgress:progress animated:YES];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didFailWithError");
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self saveLocally:_videoData res:_resourceName type:_resourceType];
NSLog(@"File Saved !");
}

最佳答案

下载屏幕 -> 上一个屏幕 -> 下载屏幕。让我们试试:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
[_videoData appendData:data];
float progress = (float)[_videoData length]/(float)_length;
NSLog(@"Progress:%0.2f",progress);
NSNumer *n = [NSNumber numberWithFloatValue:progress];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_DOWNLOAD_PROGRESS object:n];

//_timeLabel.text = [NSString stringWithFormat:@"%0.2F%%",progress*100];
[_progressView setProgress:progress animated:YES];
}

当你回到下载界面时,你应该通过接收通知来更新进度

#define  NOTIFICATION_DOWNLOAD_PROGRESS @"NOTIFICATION_DOWNLOAD_PROGRESS"
- (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processProgressBar:) name:NOTIFICATION_DOWNLOAD_PROGRESS object:nil];
}

- (void) processProgressBar:(NSNumber *) progress
{
[_progressView setProgress: progress.floatValue];
}

关于ios - 返回上一个 Controller 并再次返回时如何获取视频的下载状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23245264/

相关文章:

iphone - 带有自定义标题的 UIBarButtonItem

ios - 如何以编程方式从 UIViewController 转到 UITableViewController

ios - 如何在ios框架中覆盖值/文件

ios - 缺少约束自动布局警告

ios - 在应用内购买促销代码

ios - SQLCipher:使用未解析的标识符 'sqlite3_key'

ios - Objective C 中的 NSURL 问题

iphone - 旋转设备时如何更换新 Nib ?

objective-c - 将 Swift 中的 NSURL 方案与字符串进行比较

ios - 神奇记录自定义核心数据迁移不拉现有数据