2013-08-27 3 views
0

서버에서 사진을 다운로드하는 앱이 있습니다. 진행 상황을 사용자에게 보여주고 싶습니다. Apple docs에 관한 UIProgressView에 대해 읽었으며이 사이트에서 많은 답변을 읽었지만 제대로 작동하지 않습니다. 여기에 내가 UIProgressView를 표시 한 다음 이미지의 예상 크기를 얻을 내 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response에서UIProgressView가 표시되지 않습니다.

_profileProgressView.hidden= YES; 
_profileProgressView.progress = 0.0; 

을 내 viewDidLoad에서 내 코드 입니다. 내 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d에서

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
     self.profileProgressView.hidden= NO; 
     [self.remote_response setLength:0]; 
     received = 0; 
     self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]]; 
     NSLog(@"Content Length::%@", self.filesize); 
    } 

, 내가 다운로드 한 이미지의 비율을 계산 한 후 UIProgressView의 진행 상황을 업데이트하고 다운로드 진행 상황을 기록합니다.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d { 
     [self.remote_response appendData:d]; 

     NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.remote_response length]]; 
     dispatch_async(dispatch_get_main_queue(),^{ 
       float progress = (_profileProgressView.progress + ([self.filesize floatValue]/[resourceLength floatValue])*100); 
       [self.profileProgressView setProgress:progress animated:YES]; 
       NSLog(@"Downloading %.0f%% complete", _profileProgressView.progress); 
     }); 
    } 

이 코드를 실행

  1. UIProgressView 결코

  2. 내가 얻을 로그는 Downloading 0% complete입니다까지 보여줍니다. 다운로드중인 이미지의 로그를 가져올 것으로 예상됩니다. 내가 UIProgressView를 얻을 수행 할 때

또한 나는 viewDidDownload 내가해야 allocinit UIProgressView 생각하지만 진행률을 표시하지 않고 이미지가 다운로드되고 완료 후에는 숨기지 않습니다.

+0

여기 왜 메인 큐로 디스패치하고 있습니까? 델리게이트 응답은 메인 스레드에서 호출되기 때문에 그렇게하지 않아도됩니다. 또한 진행률은 0에서 1.0까지이므로 왜 100을 곱하는 지 알 수 없습니다. 또한 스토리 보드에서 드래그하고 있습니까? 그런 다음 alloc 및 init을 수행 할 필요가 없습니다. – AdamG

+0

progressView 개체가 nil인지 여부를 확인하십시오. –

+1

완료 했습니까 [self.view bringSubviewToFront : _profileProgressView]; ?? @ 팬디 – Learner

답변

1

당신은 NSNumber로 데이터 유형을 복싱 및 언 박싱으로 계산하는 것을 끝내 었다고 생각합니다.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    fileLength = [response expectedContentLength]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d { 
    double length = [d length]; 
    currentLength += length; 
    double progress = currentLength/fileLength; 

    if (lastProgress < progress) { 
    profileProgressView.progress = progress; 
    lastProgress = progress; 
    } 
} 
+0

NSLog lastProgress 코드를 사용하면 즉시 1.0이 생성됩니다. – nupac

+0

opps ... 행을 놓쳤습니다. 길이는 [d 길이] 여야 이중으로 편집해야합니다. – rogchap

+0

나는 여전히 같은 것을 얻지 만 내 이미지가 너무 작기 때문에이 문제가 있다고 생각한다. – nupac

0

내가 너무 이전과 같은 문제가 발생 :

double fileLength; 
double lastProgress; 
double currentLength; 

그런 다음 간단한 수학을 : 나는 어떻게 할 것인지

세 가지 변수를 만드는 것입니다. 이 경우 정확한 진도보기를 표시 할 수 없습니다. 정확한 진행 상황을 표시하려면 서버가 응답 헤더에 전송 된 바이트를 제공해야합니다. 그러면 oly, 당신의 연결에서 정확한 데이터를 얻을 수 있습니다 : didReceiveData : delegate. 그렇지 않으면 즉시 1.0이 생깁니다.

서버 의존성이없는 진행률보기를 표시하려는 경우에도 데이터 길이를 n 단계로 나누고 NSTimer를 사용하여 진행할 수 있습니다.

+0

헤더'Content-Length'가 이미 전체 크기를 가지고 있지 않습니까? – nupac