【发布时间】: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的范围内?
标签: ios objective-c uiimageview uicollectionview uiactivityindicatorview