私はいくつかの写真をダウンロードしており、進行状況バーを作成しました。 NSURLConnectionで非同期モードを使用しており、同時に約15枚のダウンロードを開始します。開始時に私はdidStartLoadImages
と呼び出し、バーは画面上で0幅に設定されます。フレームサイズはNSURLConnectionデリゲートから呼び出された直後に更新されませんか?
問題が始まると、イメージの1つが完成するとdidLoadImageWithTotalPercentCompleted:
がコールされ、現在のパーセントでバーが更新されます。それは完璧に動作し、ログはうまく書いています。フレームを更新する:20%など。しかし、ユーザーインターフェイスは更新されず、すべての画像が完成していますか?
私は、メインスレッドが非同期であってもブロックされていることに気付きましたか?
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からfloatに 'didLoadImageWithTotalPercentCompleted:(int)percent'で変更したときに、今すぐ完成します。私は間違いを見たことがないだろう。ありがとう! – David