如何运行 activity 指标直到下载图片完成
How to run activity indicator till downloading the image is Complete
我有一个 UITableView
,每个 cell
都有一个图像。当我点击 cell
时,图像将被下载。我想显示 activity 指示器显示直到图像未下载。
这是代码
TableView代码
-(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];
下载前
正在下载
当您创建图像单元格时,您需要检查图像是否已下载,如果是,则可以点击图像,否则会在其上启动 ActivityIndicator,当您完成图像下载时,您可以停止 ActivityIndicator 以获得最佳图像下载体验使用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 Link : 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
});
});
希望对您有所帮助。
使用此方法在图像下载块代码之前开始动画。
[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) {
}
}];
我有一个 UITableView
,每个 cell
都有一个图像。当我点击 cell
时,图像将被下载。我想显示 activity 指示器显示直到图像未下载。
这是代码
TableView代码
-(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];
下载前
正在下载
当您创建图像单元格时,您需要检查图像是否已下载,如果是,则可以点击图像,否则会在其上启动 ActivityIndicator,当您完成图像下载时,您可以停止 ActivityIndicator 以获得最佳图像下载体验使用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 Link : 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
});
});
希望对您有所帮助。
使用此方法在图像下载块代码之前开始动画。
[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) {
}
}];