在 UIImageView 中显示活动指示器
Posted
技术标签:
【中文标题】在 UIImageView 中显示活动指示器【英文标题】:showing activity indicator within UIImageView 【发布时间】:2016-07-18 12:00:11 【问题描述】:当我从 url 下载内容时,我正在尝试将活动指示器添加到 imageView 中,但问题是活动指示器不可见。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = nil;
if ([imageArray count] >0)
if(cachedImage[[imageArray objectAtIndex:indexPath.row]] != nil)
recipeImageView.image = cachedImage[[imageArray objectAtIndex:indexPath.row]];
else
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(140, 236, 37, 37);
[activityIndicator startAnimating];
[recipeImageView addSubview:activityIndicator];
self.view.userInteractionEnabled = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void)
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
dispatch_sync(dispatch_get_main_queue(), ^(void)
recipeImageView.image = image;
[activityIndicator stopAnimating];
cachedImage[[imageArray objectAtIndex:indexPath.row]] = image;
);
);
如果可能的话,我怎样才能在 imageview 中显示带有进度百分比的活动指示器?
【问题讨论】:
activityIndicator.center = recipeImageView.center;
试试这个。
@EktaMakadiya 这行不通,因为中心不在同一个坐标系中。
@Learner 检查我的答案。
嗨,使用 github 上的一个名为 AsyncImageView 的库,它完全可以实现您想要实现的目标。你给它 url,它显示指示器,下载和显示图像,有一个占位符图像,并且是可定制的。如果您愿意,它甚至可以存储图像!
是CGRectMake(140, 236, 37, 37)
的位置,即使在UIImageView
的范围内?
【参考方案1】:
只需更新您的代码并尝试此操作
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = nil;
if ([imageArray count] >0)
if(cachedImage[[imageArray objectAtIndex:indexPath.row]] != nil)
recipeImageView.image = cachedImage[[imageArray objectAtIndex:indexPath.row]];
else
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityIndicator setCenter: recipeImageView.center];
[activityIndicator startAnimating];
[cell.contentView addSubview:activityIndicator];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void)
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
dispatch_sync(dispatch_get_main_queue(), ^(void)
recipeImageView.image = image;
[activityIndicator removeFromSuperview];
cachedImage[[imageArray objectAtIndex:indexPath.row]] = image;
);
);
【讨论】:
男人不要复制和发布答案。请尝试给出示例或尝试代码解决方案。 我在其他 *** 相关问题中看到了您发布的答案。 我刚刚确认了一个答案。但没有从那里复制,因为我在我的应用程序中做了同样的事情。并且看他的代码正在运行,因为他对我的答案投了赞成票。 :) 当我们看到答案时,它至少应该是独一无二的。这就是我们说的原因。 是的,我同意,但有时我们必须复制问题并更新它。但从下一次我会照顾它【参考方案2】:试试这个:
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = recipeImageView.center;
[activityIndicator startAnimating];
[recipeImageView addSubview:activityIndicator];
self.view.userInteractionEnabled = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void)
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
dispatch_sync(dispatch_get_main_queue(), ^(void)
recipeImageView.image = image;
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
cachedImage[[imageArray objectAtIndex:indexPath.row]] = image;
);
);
【讨论】:
@Learner 请回复。如果这可行,请接受它。【参考方案3】: UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[indicator startAnimating];
[indicator setCenter:self.recipeImageView.center];
[self.contentView addSubview:indicator];
【讨论】:
它可以工作,但是当只有三个图像正在加载时,它会添加六个微调器以上是关于在 UIImageView 中显示活动指示器的主要内容,如果未能解决你的问题,请参考以下文章