【发布时间】:2012-01-18 13:34:31
【问题描述】:
我正在下载一些图片并创建了一个进度条。我正在使用 NSURLConnection 的异步模式并同时启动大约 15 张图片下载。在启动过程中,我调用didStartLoadImages 并且栏已成功设置为屏幕上的 0 宽度。
现在问题开始了,当其中一张图片完成后,它将调用didLoadImageWithTotalPercentCompleted: 并使用当前百分比更新栏。它工作得很好,日志写得很好 将帧更新为:20% 等。但是用户界面不会更新直到所有图像都完成?
我只是注意到主线程被阻塞了,即使它是异步的?
连接.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];
}
}
进度条.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];
}
【问题讨论】:
标签: objective-c ios ios5 nsurlconnection