1

当从一个视图移动到另一个视图时,我希望在我的第二个视图中,只要正在下载图像以查看微调器(这样第二个视图将立即打开而不需要任何时间,并且用户等待微调器结束)。

这是我的代码:

在第二个视图中,打开的那个:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIActivityIndicatorView *spinner=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    spinner.center=CGPointMake(160.0,240.0 );
    spinner.hidesWhenStopped=YES;
    [self.view addSubview:spinner];
    [spinner startAnimating];

NSString *urlString=[NSString stringWithFormat:@"http://mysite.com/projects/test/test.jpg"];

    NSLog(@"URL is:%@",urlString);
    NSURL *url=[NSURL URLWithString:urlString];
    [more_info_image setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:url]]];

    [spinner stopAnimating];
}

我根本看不到微调器。我不认为这是因为我的互联网连接太胖了。另一方面,当按下按钮并等待视图打开时,我看到了一点延迟——就像它尝试下载图像的时候一样。

我想打开视图,拥有微调器,下载图像,然后让微调器消失。

我必须改变什么吗?

4

4 回答 4

5

UIActivityIndicatorView由于您[NSData dataWithContentsOfURL:url]在主线程上使用,您可能看不到。这会阻止主线程显示,UIActivityIndicatorView直到图像下载完成,然后您将其删除。

您可能想要执行以下操作:

但请确保您在文件中定义spinner*.h此功能。

- (void)viewDidLoad
{
   ...

   //This code will make downloadImage run in the background thread
   [self performSelectorInBackground:@(downloadImage) withObject:nil]

   ...
}

- (void) downloadImage{
    NSLog(@"URL is:%@",urlString);
    NSURL *url=[NSURL URLWithString:urlString];
    UIImage *downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

    //This code will make setImage run in the main thread since it is changing UI
    [more_info_image performSelectorOnMainThread:@selector(setImage:) withObject:downloadedImage waitUntilDone:NO];
    //This code will make stopAnimating run in the main thread since it is changing UI
    [spinner performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];

}

另一种方法是使用Grand Central Dispatch并执行以下操作:

- (void)viewDidLoad
{

    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIActivityIndicatorView *spinner=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    spinner.center=CGPointMake(160.0,240.0 );
    spinner.hidesWhenStopped=YES;
    [self.view addSubview:spinner];
    [spinner startAnimating];

    //This is the new GCD code
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
    dispatch_async(queue, ^{
        //This code will run on a background thread
        NSString *urlString=[NSString stringWithFormat:@"http://mysite.com/projects/test/test.jpg"];

        NSLog(@"URL is:%@",urlString);
        NSURL *url=[NSURL URLWithString:urlString];
        UIImage *downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

        dispatch_sync(dispatch_get_main_queue(), ^{
            //this code runs on the main thread since it is UI changes
            [more_info_image setImage:downloadedImage];

            [spinner stopAnimating];
        });
    });
}
于 2012-12-28T15:11:16.750 回答
2

当您在 viewDidLoad 方法中下载图像时,因此在下载图像时它没有加载您的视图,这就是您遇到延迟的原因,试试这个

在你的头文件中添加一个 UIActivityIndi​​cator 的实例

UIActivityIndicator *spinner;

在你的实现文件中做这样的事情

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    spinner=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    spinner.center=CGPointMake(160.0,240.0 );
    spinner.hidesWhenStopped=YES;
    [self.view addSubview:spinner];
    [spinner startAnimating];
    [self performSelector:@selector(downloadYourImageMethod) withObject:nil afterDelay:1];
}

-(void)downloadYourImageMethod
{
    NSString *urlString=[NSString stringWithFormat:@"http://mysite.com/projects/test/test.jpg"];
    NSURL *url=[NSURL URLWithString:urlString];
    [myIMageview setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:url]]];

    [spinner stopAnimating];
}
于 2012-12-28T15:14:58.613 回答
1

将您的代码修改为

    - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIActivityIndicatorView *spinner=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    spinner.center=CGPointMake(160.0,240.0 );
    spinner.hidesWhenStopped=YES;
    [self.view addSubview:spinner];
    [spinner startAnimating];
     [self performSelector:@selector(downloadImage) withObject:nil afterDelay:0.1];
}


-(void)downloadImage
{
NSString *urlString=[NSString stringWithFormat:@"http://mysite.com/projects/test/test.jpg"];

    NSLog(@"URL is:%@",urlString);
    NSURL *url=[NSURL URLWithString:urlString];
    [more_info_image setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:url]]];

    [spinner stopAnimating];
}
于 2012-12-28T15:09:49.933 回答
0

有时,这完全取决于您如何放置您的视图。微调器应该是添加到层次结构中的最后一个东西,以便在视图加载时成为顺序中的第一个。这是我的问题,我希望这可以帮助其他遇到这个答案的人。:)

于 2013-11-28T01:07:38.970 回答