我正在下載一些圖片並創建了一個進度條。我在NSURLConnection中使用異步模式,並同時啓動約15張圖片下載。在開始的過程中,我打電話didStartLoadImages
,並且屏幕上的條已成功設置爲0寬度。從NSURLConnection委託調用後,幀大小不會直接更新?
現在問題開始,當其中一個圖像完成後,它會調用didLoadImageWithTotalPercentCompleted:
並用當前百分比更新條形圖。它完美的工作和日誌寫得很好更新幀:20%等。但用戶界面不更新UNTILL所有的圖像都完成?
我只是注意到,即使它是異步的主線程被阻止?
Connection.m
-(void)loadImage:(NSString*)imageName numberOfImagesInSecquence:(int)nrOfImages {
NSString *url = [NSString stringWithFormat:@"https://xxx.xxx.xxx.xxx/~%@/files/%@",site,imageName];
/* Send URL */
NSURL *urlToSend = [[NSURL alloc] initWithString:url];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:urlToSend];
theConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
self.receivedData = [NSMutableData data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
connectionIsLoading = NO;
[[self delegate] didLoadImagesWithProcent:((float)1 - ((float)[imageArray count]/(float)nrOfImages)) *(float)100];
}
MainViewController.m
-(void)didStartLoadImages {
/* Sets progress bar to 0% */
[progressBar setBarWithPercent:0];
}
-(void)didLoadImageWithTotalPercentCompleted:(int)percent {
if (percent == 100) {
/* Done */
} else {
[progressBar setBarWithPercent:percent];
}
}
ProgressBar.m
-(void)setBarWithPercent:(float)percent {
int maxSizeOfBar = 411;
[UIView beginAnimations:@"in" context:NULL];
[UIView setAnimationDuration:0.2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationBeginsFromCurrentState:YES];
CGRect frame = bar.frame;
frame.size.width = maxSizeOfBar * (percent/100) + 10;
bar.frame = frame;
NSLog(@"Updating frame to: %i",percent);
[UIView commitAnimations];
}
你救了我的一天!當我從int更改爲'didLoadImageWithTotalPercentCompleted:(int)percent'中的float時,現在可以工作。我永遠不會看到那個錯誤。謝謝! – David 2012-01-18 14:11:06