开发者

Loading Multiple Images from Server on UIButtons??

i am trying to upload 8-10 thumbnail images on UIButton. The images are loaded directly via Links to my server for eg. www.abc.com/fire.png. I have placed these UIButtons inisde a Scrollview. the following code is how i achieve it. but whenever this method gets called my applications hangs up as i guess the ima开发者_运维百科ges take time to load on the buttons. How can i stop my app from getting stuck for few seconds??

- (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];
}


You should try to fetch these images (preferably in a background thread) before the scroll view is displayed. Save these on the disk & use them in the scroll view. A network operation will generally not be that fast that you don't observe a lag.

HTH,

Akshay


To avoid freezing you have to go with NSOperationQueue. Here is a good and appropriate example...


You can use threads so that download the image in the background thread & cache it.So that next time your performance will be good.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜