在 UIImageView 中显示 activity 指示符

showing activity indicator within UIImageView

当我从 url 下载内容时,我正尝试将 activity 指示器添加到 imageView 中,但问题是 activity 指示器不可见。

- (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 中显示带有进度百分比的 activity 指示器?

 UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

[indicator startAnimating];
[indicator setCenter:self.recipeImageView.center];
[self.contentView addSubview:indicator];

只需更新您的代码并试试这个

- (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;
                });
            });
        }
    }

试试这个:

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;
            });
        });