当应用程序启动时,我将 UIImageViews 添加到没有图像的屏幕上。然后我使用一张一张地加载图像NSThread
并将其设置为那些视图。UiImageViews 保持空白,直到所有图像都已加载并且线程完成。为什么我设置后图像不显示UIImageView
,为什么要等待NSThread
结束?
添加图像视图:
for(i = value; i < val; i++){
UIButton *newbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[newbutton setBackgroundColor:[UIColor whiteColor]];
UIImageView *newback = [[UIImageView alloc] init];
newback.contentMode = UIViewContentModeScaleAspectFit;
[newback setFrame:CGRectMake(0, 0, 140, 140)];
[newbutton addSubview:newback];
height = MIN(300, newSize.height);
[newbutton setFrame:CGRectMake(currentX, currentY, width, height)];
newbutton.layer.cornerRadius = 10;
newbutton.clipsToBounds = YES;
[newbutton addTarget:self action:@selector(processButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
要在线程中添加图像,我调用了一个执行以下操作的函数:
for(i = value; i < val; i++){
UIImage *newimage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@“http://test.com/a.jpg”, hash]]]];
UIButton *newbutton = (UIButton*)[self.subviews objectAtIndex:i];
[((UIImageView*)[newbutton.subviews objectAtIndex:0]) newimage];
}