我有 60 的滚动视图UIImageView's
。这些图像视图中显示的图像来自 url 和我从 web 服务获得的 url。当用户滚动到底部时,我调用 web 服务并获取新的 60 urls。获得网址后,我使用相同的网址UIImageView's
显示图像。以下是我显示图像的代码。
for (UIView *viewSel in [scrlView subviews]) {
NSString *strImgUrl = [arrImgURL valueForKey:@"url"];
if ([viewSel isKindOfClass:[UIImageView class]]) {
UIImageView *imgView = (UIImageView *)viewSel;
[imgView setImage:[UIImage imageNamed:@"loading432x520.png"]];
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:[NSString stringWithFormat:@"%@",strImgUrl]];
[arr addObject:imgView];
[self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr];
[arr release];
}
}
- (void) loadImageInBackground:(NSMutableArray *)arr {
NSLog(@"loadImage");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[arr objectAtIndex:0]]];
UIImage *img = [[UIImage alloc] initWithData:imgData];
if (img != nil) {
[arr addObject:img];
}
else{
[arr addObject:[UIImage imageNamed:@"no-image432x520.png"]];
}
[img release];
[self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
[pool release];
}
- (void) assignImageToImageView:(NSMutableArray *)arr{
NSLog(@"assignImage");
UIImageView *imgView = [arr objectAtIndex:1];
imgView.image = [arr objectAtIndex:2];
}
该代码第一次完美运行。但是当我得到新的网址时,它正在工作一段时间或崩溃。我不知道为什么它会崩溃。我想阻止那场崩溃。如果您没有回答我的问题,请告诉我。请帮助我。提前致谢。有效的答案将不胜感激。