【问题标题】:Loading Multiple Images from Server on UIButtons??在 UIButtons 上从服务器加载多个图像?
【发布时间】:2011-08-09 06:54:46
【问题描述】:

我正在尝试在 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];
}

【问题讨论】:

  • 我面临着同样的问题,那么哪一个是解决这些问题的最佳解决方案.. 延迟加载或 NSOperationQueue?任何人都可以给我一些指导。

标签: iphone ios uibutton


【解决方案1】:

您应该在显示滚动视图之前尝试获取这些图像(最好在后台线程中)。将这些保存在磁盘上并在滚动视图中使用它们。网络操作通常不会那么快,以至于您不会观察到延迟。

HTH,

阿克谢

【讨论】:

    【解决方案2】:

    为避免冻结,您必须使用 NSOperationQueue。 Here 是一个很好的例子……

    【讨论】:

      【解决方案3】:

      您可以使用线程,以便在后台线程中下载图像并缓存它。这样下次您的性能会很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-14
        • 1970-01-01
        • 2013-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多