0
您好新多線程....下面是我寫的代碼在單獨的線程中下載視頻的代碼,但委託方法沒有引發任何人請幫助我解決這個.. 。在此先感謝..委託方法不在iOS中的多線程程序中觸發
- (void)downLoad {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_videoData = [[NSMutableData alloc] init];
[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://km.support.apple.com/library/APPLE/APPLECARE_ALLGEOS/HT1211/sample_iTunes.mov"]] delegate:self];
//saving is done on main thread
});
}
- (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 {
dispatch_async(dispatch_get_main_queue(), ^{
[self saveLocally:_videoData];
NSLog(@"File Saved !");
});
}