2017-01-21 5 views
0

プログレスバーを設定したいが、私のURLは私のイメージの合計サイズを与えることはできない。 totalBytesExpectedToWriteが-1の場合、それはあなたのイメージの合計サイズが応答していないので、私は、あなたがあなたのサーバーの構成上のいくつかの問題を持って、このチュートリアルにhttp://tarikfayad.com/afnetworking-downloading-files-with-progress/Afnetworking 3.0合計バイトが書き戻しに返される-1

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

//Most URLs I come across are in string format so to convert them into an NSURL and then instantiate the actual request 
NSURL *formattedURL = [NSURL URLWithString:@"http://www.wallpapers13.com/wp-content/uploads/2016/04/Sunset-Large-Chinese-wall-Desktop-Wallpaper-HD-5250x3344.jpg"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:formattedURL]; 

[manager setDownloadTaskDidWriteDataBlock:^(NSURLSession * _Nonnull session, NSURLSessionDownloadTask * _Nonnull downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) { 
    //CGFloat w = totalBytesWritten; 
    CGFloat t = totalBytesExpectedToWrite; 
    //CGFloat percentageCompleted = w/t; 
    NSLog(@"%f",t); // Error retun -1 
}]; 
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { 
    // 
    //Getting the path of the document directory 
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; 
    NSURL *fullURL = [documentsDirectoryURL URLByAppendingPathComponent:@"image1.jpg"]; 
    //If we already have a video file saved, remove it from the phone 
    [self removeVideoAtPath:fullURL]; 
    return fullURL; 
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { 
    if (!error) { 
     //If there's no error, return the completion block 
     [self completionBlock:(NSString *)filePath]; 
    } else { 
     //Otherwise return the error block 
     [self errorBlock:error]; 
    } 
}]; 
[download resume]; 
+0

を...'ここにコードhttps://gist.github.com/andreortiz82をブロックするためのリンクです/ 2934606 – Nazir

+0

この投稿には何の質問もありません。何を達成したいのですか?何が助けになるのですか?元の投稿を編集してください! – ingaham

+0

@ Nazirサーバーからダウンロードしたい、アップロードしない。私たちがダウンロードするとき、私たちはダウンロードコンテンツの量を知ることができません。あなたはこれについて私を助けることができますか? –

答えて

1

このお試しください: `[操作setUploadProgressBlock:あなたはプログレスバートライをしようとしている場合

NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

NSURL * URL = [NSURL URLWithString:@"http://static1.visitfinland.com/wp-content/uploads/Header_Linnunlaulu_ruska_autumn.jpg"]; 
NSURLRequest * request = [NSURLRequest requestWithURL:URL]; 

NSURLSessionDownloadTask * downloadTask = [manager downloadTaskWithRequest:request 
                    progress:^(NSProgress * _Nonnull downloadProgress) { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     // here you will be able to set progress to your progress view 
     [yourProgressView setProgress:downloadProgress.fractionCompleted]; 
    }); 
} destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { 
    NSURL * documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory 
                      inDomain:NSUserDomainMask 
                    appropriateForURL:nil 
                      create:NO 
                       error:nil]; 

    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; 
} completionHandler:^(NSURLResponse * response, NSURL * filePath, NSError * error) { 

}]; 

[downloadTask resume]; 
+0

この解決策は問題ありません。私のプロジェクトではAFNetworking 3.0を使用することが義務付けられています。 –

+0

はい、AFNetworking 3.0とAFURLSessionManagerが使用されている場合は、こちらもサンプルです:https://github.com/AFNetworking/AFNetworking。 – Nazir

+0

私はそれに感謝しました。 @ナジール –

1

に従ってください。

合計サイズがなければ、正しい進行状況バーを表示することはできません。アクティビティインジケータは表示されるだけです(何か起こっていることを示していますが、終了時にフィードバックは表示されません)。

サーバーの設定(特に、正しい方法でヘッダーを設定する)を確認することをお勧めします。

+0

私はプロセスのバーを行うことができない合計サイズなしで知っています。サーバーの問題は何ですか?あなたは説明できますか? –

+0

あなたの質問は何ですか? – ingaham

+0

あなたはサーバーの設定を手伝ってくれますか?私はcpanelを使用するので、詳細な手順を教えてください。 –

関連する問題