我正在尝试在 UIButton 上上传 8-10 个缩略图。图像通过链接直接加载到我的服务器,例如。 www.abc.com/fire.png。我已将这些 UIButtons 放置在 Scrollview 中。以下代码是我如何实现它的。但是每当调用此方法时,我的应用程序都会挂起,因为我猜图像需要时间才能加载到按钮上。我怎样才能阻止我的应用程序卡住几秒钟??
- (void)loadSCRView
{
[self.view addSubview:scrollView];
for (i = 0; i < [myEngine.logoUrlArr count]; i++)
{
NSString *imageName = [NSString stringWithFormat:@"%@",[myEngine.logoUrlArr objectAtIndex:i]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:imageName] options:NSDataReadingUncached error:nil]];
UIButton *btn = [[UIButton alloc]init];
[btn setImage:image forState:UIControlStateNormal];
[btn setFrame:CGRectMake(x, 4, 57, 57)];
x=x+ 70.00;
btn.tag = i;
[scrollView addSubview:btn];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];
UILabel *label = [[UILabel alloc]init];
label.text = [myEngine.nameArr objectAtIndex:i];
[label setFont:[UIFont fontWithName:@"Helvetica" size:12]];
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[label setFrame:CGRectMake(btn.frame.origin.x , 59, 70, 20)];
//[label setFrame:CGRectMake(100 , 59, 70, 20)];
y=y+ 100.00;
[scrollView addSubview:label];
[btn release];
[label release];
}
最佳答案
您应该尝试在显示 ScrollView 之前获取这些图像(最好在后台线程中)。将它们保存在磁盘上并在 ScrollView 中使用它们。网络操作通常不会那么快,您不会观察到延迟。
HTH,
阿克谢
关于iphone - 在 UIButtons 上从服务器加载多个图像??,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6992445/