2016-04-01 15 views
1

サーバーからファイルをダウンロードします。ファイルがダウンロードされている間に、テーブルのセルの進行状況を表示したい。これどうやってするの?テーブルセルの進行状況を表示するには?

ファイルのダウンロードコード[NSData dataWithContentsOfUrl]では不可能だ

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"01.mp3"]; 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:NO]; 

if (!fileExists) { 

    NSString *stringURL = @"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39X0FOaUJXVDUwOHc"; 
    NSURL *url = [NSURL URLWithString:stringURL]; 
    NSData *urlData = [NSData dataWithContentsOfURL:url]; 
    [urlData writeToFile:filePath atomically:YES]; 

} 
}); 
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil]; 
} 

答えて

0

。あなたのビューコントローラでのスタートのダウンロード、およびアクセス進捗

enter image description here

それにプログレスバーを追加するカスタムセルXIBファイルを作成します How to get download progress in AFNetworking 2.0?

0

を参照してください、このようなAFNetworking

としてそのためのライブラリが必要になりますバーを開き、進行状況をでした.ReceiveDataメソッド

私はちょうどv IEWコントローラは、あなたが

テーブルビューの方法でそれを使用する必要があるコードサンプル:私は `NSURLRequest *のURLRequest = [NSURLRequestを使用する場合

@interface ViewController()<NSURLConnectionDataDelegate> 

    //Put these in custom cell file 
    @property (weak, nonatomic) IBOutlet UIProgressView *progressView; 
    @property (weak, nonatomic) IBOutlet UIImageView *imageView; 

    @property (strong, nonatomic) NSURLConnection *connectionManager; 
    @property (strong, nonatomic) NSMutableData *downloadedMutableData; 
    @property (strong, nonatomic) NSURLResponse *urlResponse; 

    @end 

    @implementation ViewController 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     //Start download request logic 
     self.downloadedMutableData = [[NSMutableData alloc] init]; 
     NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.planwallpaper.com/static/images/63906_1_BdhSun5.jpg"] 
                cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
               timeoutInterval:60.0]; 
     self.connectionManager = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
    } 



    #pragma mark - Delegate Methods 
    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
     NSLog(@"%lld", response.expectedContentLength); 
     self.urlResponse = response; 
    } 

//Show progress download while receiving data 
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
     [self.downloadedMutableData appendData:data]; 
     self.progressView.progress = ((100.0/self.urlResponse.expectedContentLength)*self.downloadedMutableData.length)/100; 
     if (self.progressView.progress == 1) { 
      self.progressView.hidden = YES; 
     } else { 
      self.progressView.hidden = NO; 
     } 
     NSLog(@"%.0f%%", ((100.0/self.urlResponse.expectedContentLength)*self.downloadedMutableData.length)); 
    } 

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection { 
     NSLog(@"Finished"); 
     self.imageView.image = [UIImage imageWithData:self.downloadedMutableData]; 
    } 

結果は

enter image description here

+0

のように見えますrequestWithURL:[NSURL URLWithString:@ "https://drive.google.com/uc?export=download&id=0B6zMam2kAK39Zm5LUTctZXNLUlE»]'進行状況が機能しません。どうして? – user

+0

**私たちがダウンロードの進捗状況を得るためのデリゲートメソッドが必要** ** NSURLConnection **クラスを使用していました。** didReceiveData **デリゲートメソッドがあります。ここで私はプログレスバーを更新しています – swiftBoy

関連する問題