当回到以前的控制器并再次回来时如何获得视频的下载状态?

时间:2014-04-23 13:07:23

标签: ios objective-c

在这里下载视频并将其存储到iphone中的文档文件夹...但是当我点击下载按钮视频将开始下载并且进度将很好地显示...但是当我回到以前的视图控制器并且来了在下载过程中回到下载控制器进度视图状态将不会显示当前状态...有人可以帮我解决这个问题吗?提前谢谢......

这是我的代码......

- (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 !");
}

1 个答案:

答案 0 :(得分:1)

下载屏幕 - >上一个屏幕 - >下载屏幕。试试吧:

-(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];
}