서버에서 사진을 다운로드하는 앱이 있습니다. 진행 상황을 사용자에게 보여주고 싶습니다. 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);
});
}
이 코드를 실행
는UIProgressView 결코
내가 얻을 로그는
Downloading 0% complete
입니다까지 보여줍니다. 다운로드중인 이미지의 로그를 가져올 것으로 예상됩니다. 내가 UIProgressView를 얻을 수행 할 때
viewDidDownload
내가해야
alloc
및
init
UIProgressView 생각하지만 진행률을 표시하지 않고 이미지가 다운로드되고 완료 후에는 숨기지 않습니다.
여기 왜 메인 큐로 디스패치하고 있습니까? 델리게이트 응답은 메인 스레드에서 호출되기 때문에 그렇게하지 않아도됩니다. 또한 진행률은 0에서 1.0까지이므로 왜 100을 곱하는 지 알 수 없습니다. 또한 스토리 보드에서 드래그하고 있습니까? 그런 다음 alloc 및 init을 수행 할 필요가 없습니다. – AdamG
progressView 개체가 nil인지 여부를 확인하십시오. –
완료 했습니까 [self.view bringSubviewToFront : _profileProgressView]; ?? @ 팬디 – Learner