iphone - 在 UIButtons 上从服务器加载多个图像??

标签 iphone ios uibutton

我正在尝试在 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/

相关文章:

ios - 只有 iOS 无法连接到 firebase

iOS13 UIDocumentPickerViewController - 打开目录不显示选择/打开/取消按钮

ios - 在不使用 CollectionView 的情况下在 GridView 中列出 UIButton

ios - ScrollView 中的移动按钮 - iOS

iphone - 何时在 iPhone 中使用同时具有静态方法和实例方法的类

iphone - iOS CoreData 更新实体对象

ios - Ionic v1 安全区域无法在 Xcode 11.5 模拟器上运行

ios - 我可以停止在特定时间间隔内快速接收推送通知吗

iOS9导致iPhone 5优化丢失

objective-c - 设置 UIButton 的标题