在iPhone中向滚动视图添加多个按钮时,如何避免延迟? [英] how to avoid delay when adding more than one button to scrollview in iPhone?

查看:44
本文介绍了在iPhone中向滚动视图添加多个按钮时,如何避免延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在UIScrollview中添加一个以上的按钮(取决于数组数量),现在我正在使用以下代码.此代码可以正常工作,但是使用此功能需要更多时间(添加按钮的延迟).请帮助我..

I need to add more than one button (its depend upon array count) to UIScrollview.Now i am using the following code.This code is working properly but more time ( delay for adding button) taking for this function.Please help me..

   for (int i=0; i< [imageArray count]; i++) {
    NSURL *url = [NSURL URLWithString:[[imgArray objectAtIndex:i]objectForKey:@"url"]];
    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(xp, 0, 75, 75);
    [button1 setImage:img forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    button1.layer.borderColor = [UIColor whiteColor].CGColor;
    button1.layer.borderWidth = 2.0;
    [mScrollView addSubview:button1];
    xp += button1.frame.size.width+15;
}

推荐答案

因为您是从服务器加载映像,所以它将阻塞您的主线程,直到映像完全加载为止.尝试在其他线程中加载图像

Because you are loading your image from server so it blocks your main thread till image load itself completely. Try loading image in different thread

下面是显示如何在不同线程上加载图像的示例

Below is an example that shows how to load image on diffrent thread

将您的按钮对象和网址添加到数组中(可以在for循环中编写)

Add your button object and url in an array (this can be written inside your for loop)

 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
[array addObject:cell.businessLogoImageView];
[array addObject:@"your Url"];
[NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:array];
[array release];
array = nil;

现在实现loadImage

now implement loadImage

-(void)loadImage:(NSArray *)objectArray
 {
    UIImageView *tempImageView = (UIImageView *)[objectArray objectAtIndex:0];
    tempImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[objectArray objectAtIndex:1]]]];

 }

这篇关于在iPhone中向滚动视图添加多个按钮时,如何避免延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆