2017-01-21 3 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

진행중인 바 시도를 할하려는 경우 :'[작업 setUploadProgressBlock을 ...'여기에 코드 https://gist.github.com/andreortiz82를 차단하는 링크입니다/2934606 – Nazir

+0

이 게시물에 질문이 없습니다. 무엇을 성취하고 싶습니까? 무엇이 도움이 필요합니까? 원래 게시물을 수정하십시오! – ingaham

+0

@ Nazir 서버에서 다운로드하지 않고 업로드하고 싶습니다. 우리가 다운로드 할 때 우리는 얼마나 많은 다운로드 콘텐츠를 알 수 없습니다. 그래서 이걸 도와 줄 수 있어요. –

답변

1

이 시도 :

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

을 따르십시오.

전체 크기가 없으면 올바른 진행률 표시 줄을 표시 할 수 없으며 일부 활동 표시기 만 표시 할 수 있습니다.이 표시기는 상황이 발생했음을 나타내며 완료 될 때 피드백을 제공하지 않습니다.

서버 설정 (특히 올바른 방법으로 Content-Length 헤더를 설정하는 방법)을 살펴 보는 것이 좋습니다.

+0

총 크기없이 프로세스 바를 처리 할 수 ​​없다는 것을 알고 있습니다. 서버 문제 란 무엇입니까? 설명해 주시겠습니까? –

+0

그럼 뭐가 문제 야? – ingaham

+0

서버 구성에 대해 자세히 설명해 줄 수 있습니까? 나는 cpanel을 사용하므로 자세한 단계를 알려주십시오 –