如何运行活动指示器直到下载图像为完成

时间:2016-08-15 09:19:38

标签: ios objective-c iphone nsdata uiactivityindicatorview

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

这是代码

TableView代码

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

PicturePerformGesture Code

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

下载前

正在下载
enter image description here

2 个答案:

答案 0 :(得分:3)

当您在此时创建Image Cell时,您需要检查图像是否已下载如果是可以点击图像,则可以在其上启动ActivityIndi​​cator并在完成图像下载时可以停止ActivityIndi​​cator以获得最佳图像下载体验使用SDWebImage第三方图书馆,并获得如下所述的所有图像状态。

使用完成阻止:

// Here we use the new provided sd_setImageWithURL: method to load the web image
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.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 
                    });
                });

希望它会对你有所帮助。

答案 1 :(得分:0)

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

[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) {

                        }
                    }];