3

일부 데이터를 서버에 업로드하기 위해 POST 요청을 사용 중이고 NSURLConnection의 메서드의 totalBytesWritten 속성을 기반으로 UIProgressView의 진행률을 업데이트하려고합니다. 아래 코드를 사용하면 진행률보기를 적절하게 업데이트하지 못합니다. 완료 될 때까지 항상 0.000입니다. 업로드를 더 잘 진행하기 위해 무엇을 배가하거나 나누어야할지 모르겠습니다.NSURLConnection didSendBodyData 진행

제공되는 도움에 감사드립니다. 코드 :

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite 
{ 
    NSNumber *progress = [NSNumber numberWithFloat:(totalBytesWritten/totalBytesExpectedToWrite)]; 

    NSLog(@"Proggy: %f",progress.floatValue); 

    self.uploadProgressView.progress = progress.floatValue; 
} 

답변

7

bytesWritten 및 bytesExpected를 float 값으로 캐스팅하여 나누어야합니다.

float myProgress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite; 
progressView.progress = myProgress; 

그렇지 않으면 2 정수를 나눈 결과로 0 또는 다른 숫자가 표시됩니다.

예 : 10/25 = 0

10.0/25.0 = 0.40

오브젝티브 C는 나머지를 결정하기위한 연산자 modulus%를 제공 정수 분할에 유용하다.

2

코드가 좋네요. 업로드에는 20MB에서 50MB와 같은 큰 파일을 사용해보십시오.

당신은 당신이 관련 진행 상황을 설정할 수 있습니다 UIProgressView 사용하는 경우 : 간단한 코드에서

float progress = [[NSNumber numberWithInteger:totalBytesWritten] floatValue]; 
float total = [[NSNumber numberWithInteger: totalBytesExpectedToWrite] floatValue]; 
progressView.progress = progress/total; 

:이 같은 방법 didSendBodyData : totalBytesWritten : totalBytesExpectedToWrite

progressView.progress = (float)totalBytesWritten/totalBytesExpectedToWrite 

그것이 도움이 될 것입니다 희망을 당신.