【问题标题】:How to run activity indicator till downloading the image is Complete如何运行活动指示器直到下载图像完成
【发布时间】:2016-12-21 12:03:26
【问题描述】:

我有一个UITableView,每个cell 都有一张图片。当我点击cell 时,图像将被下载。我想显示活动指示器,直到图像未下载为止。

这是代码

表格视图代码

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(PicturePerformGesture:)];
[cell.oMessageImg setUserInteractionEnabled:YES];
}

PicturePerformGesture 代码

UIActivityIndicatorView  *iActivity=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[iActivity startAnimating];
VideoData =[NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:videoUrl] returningResponse:nil error:nil];
[VideoData writeToFile:fullPath atomically:true];
 [iActivity stopAnimating];

下载前

下载时

【问题讨论】:

  • 使用 MBProgressHud github.com/jdg/MBProgressHUD
  • 发布您的TableView 代码以及下载图片的位置。
  • 请把代码贴在'PicturePerformGesture'方法中
  • @SerkanHekimoglu 我想将 UiActivity 指示器放在单元格上

标签: ios objective-c iphone nsdata uiactivityindicatorview


【解决方案1】:

当您创建图像单元时,您需要检查图像是否已下载,如果是,则可以点击图像,否则会在其上启动 ActivityIndi​​cator,当您完成图像下载时,您可以停止 ActivityIndi​​cator 以获得最佳图像下载体验,使用 SDWebImage 3rd派对库并获取所有图像状态,如下所述。

使用完成块:

// Here we use the new provided sd_setImageWithURL: method to load the web image
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.example.com/path/to/image.jpg"]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                ... completion code here ...

   // Add Your Stop Activity Code here.

}];

另一种获取图像下载进度的方法使用代码块下方:

SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:imageURL
                         options:0
                        progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                            // progression tracking code
                        }
                       completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                            if (image && finished) {
                                // do something with image
                                // Assign Image to ImageView and stop activity Indicator View
                            }
                        }];

SDWebImage 链接https://github.com/rs/SDWebImage

或者您可以使用自己的代码在GCD dispatch_async 的帮助下下载图像,如下所述。

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
                    //  You may want to cache this explicitly instead of reloading every time.
                    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"]]];
                    UIImage* image = [[UIImage alloc] initWithData:imageData];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        // Capture the indexPath variable, not the cell variable, and use that
                        UITableViewCell *blockCell = [tableView cellForRowAtIndexPath:indexPath];
                        blockCell.imageView.image = image;
                        [blockCell setNeedsLayout];
                        //Stop Your ActivityIndicator View hare 
                    });
                });

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    使用此方法在图片下载块代码之前开始动画。

    [myActivityIndicator startAnimating];
    

    并使用这样的 iOS 块在您的图像下载中停止它...

    SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
    [downloader downloadImageWithURL:imageURL
                         options:0
                        progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                            // progression tracking code
                        }
                       completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                            [myActivityIndicator startAnimating];
                            if (image && finished) {
    
                            }
                        }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多