【问题标题】:Activity indicator while downloading images iOS下载图像iOS时的活动指示器
【发布时间】:2015-02-13 22:36:32
【问题描述】:

我正在下载一组图像来创建动画。如何在下载开始时显示活动指示器以及何时完成隐藏活动指示器。

self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.width-70)];



        img.animationImages = [NSArray arrayWithObjects:

                                   [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],

                                   [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],

                                   [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],

                                   [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],




        img.animationDuration = 20.0f;

        img.animationRepeatCount = 0;

        [img startAnimating];

        [self.view addSubview: img];

【问题讨论】:

    标签: ios objective-c uiimageview uiimage activity-indicator


    【解决方案1】:

    您的图像正在同步下载,这意味着您阻塞了主线程(这很糟糕)。相反,在后台线程上运行下载,这将允许主线程以及扩展应用程序的 UI 仍然运行。使用您的示例代码,这是在后台运行时的样子:

    self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.width-70)];
    self.img.animationDuration = 20.0f;
    self.img.animationRepeatCount = 0;
    [self.img startAnimating];
    [self.view addSubview:self.img];
    
    // START ANIMATING YOUR ACTIVITY INDICATOR HERE
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
    
        NSArray *images = @[
            [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
            [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
            [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
            [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
        ];
    
        dispatch_async(dispatch_get_main_queue(), ^(void) {
    
            // STOP ANIMATING YOUR ACTIVITY INDICATOR HERE
    
            [self.img setAnimationImages:images];
    
        });
    
    });
    

    【讨论】:

      【解决方案2】:

      开始下载时编写以下代码: UIActivityIndi​​catorView *spinner = [[UIActivityIndi​​catorView alloc] initWithActivityIndi​​catorStyle:UIActivityIndi​​catorViewStyleGray] ; spinner.frame = CGRectMake(0, 0, 320, 50); [微调器启动动画];

      下载结束时编写此代码:[spinner stopAnimating];

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-21
        • 1970-01-01
        • 2013-09-04
        • 1970-01-01
        相关资源
        最近更新 更多