Question

I am trying to implement UICollectionView and show images. I am using SDWebimage which works perfectly in tableviewcells but when i tried to use it in UICollectionviewCell it doesn't stop and remove activityindicator. It does place the placeholder image if there is no downloaded image. I am not sure what is the difference between tableviewcell and collectionviewcell that might cause this problem.

Here is the code:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"personImageCell";

    PersonCollectionViewCell *cell = (PersonCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    Person *person = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSString *imgURL=[person.imageurl stringByAppendingString:@"?maxheight=300&maxwidth=400"];

    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicator.hidesWhenStopped = YES;
    activityIndicator.hidden = NO;
    [activityIndicator startAnimating];
    activityIndicator.center = CGPointMake(cell.ivPersonImage.frame.size.width /2, cell.ivPersonImage.frame.size.height/2);
    [cell.ivPersonImage setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:nil options:SDWebImageProgressiveDownload success:^(UIImage *image, BOOL cached){
        [activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
        NSLog(@"activity indicator should be removed");
    }failure:^(NSError *error){
        [activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
        cell.ivPersonImage.image = [UIImage imageNamed:@"placeholder.png"];
    }];


    [cell.ivPersonImage addSubview:activityIndicator];
    return cell;
}

UPDATE:

When i do NSLog(@"activity indicator should be removed %@,activityIndicator);

I get this output:

 activity indicator should be removed <UIActivityIndicatorView: 0xa520ab0; frame = (65 90; 20 20); hidden = YES; layer = <CALayer: 0xa520b60>> 

It shows that UIActivityindicator is hidden but it is still showing on top of the image

Was it helpful?

Solution

It seems that you are reusing cell so there are more then one UIActivityIndicatorViews.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"personImageCell";

    PersonCollectionViewCell *cell = (PersonCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    Person *person = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSString *imgURL=[person.imageurl stringByAppendingString:@"?maxheight=300&maxwidth=400"];

    UIActivityIndicatorView *activityIndicator = [cell.ivPersonImage viewWithTag:10];
    if (activityIndicator) [activityIndicator removeFromSuperview];
    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicator.hidesWhenStopped = YES;
    activityIndicator.hidden = NO;
    [activityIndicator startAnimating];
    activityIndicator.center = cell.ivPersonImage.center;
    activityIndicator.tag = 10;

    [cell.ivPersonImage addSubview:activityIndicator];
    [cell.ivPersonImage setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:nil options:SDWebImageProgressiveDownload success:^(UIImage *image, BOOL cached){
        [activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
        NSLog(@"activity indicator should be removed");
    }failure:^(NSError *error){
        [activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
        cell.ivPersonImage.image = [UIImage imageNamed:@"placeholder.png"];
    }];

    return cell;
}

OTHER TIPS

Hmm....this is weird..can you try to make sure the activityIndicator is working on the main thread -

[cell.ivPersonImage setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:nil options:SDWebImageProgressiveDownload success:^(UIImage *image, BOOL cached){
        dispatch_async(dispatch_get_main_queue(), ^(void){
            [activityIndicator stopAnimating];
            [activityIndicator removeFromSuperview];
        }
        NSLog(@"activity indicator should be removed");
    }failure:^(NSError *error){
        dispatch_async(dispatch_get_main_queue(), ^(void){
            [activityIndicator stopAnimating];
            [activityIndicator removeFromSuperview];
        }
        cell.ivPersonImage.image = [UIImage imageNamed:@"placeholder.png"];
    }];

I suspect it is not, that is why it is not stopping its animation.

Create the activity indicator in the ViewDidLoad of current View Controller

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView      alloc]initWithFrame:CGRectMake(150, 225, 20, 30)];
[spinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
spinner.color = [UIColor blueColor];
[self.view addSubview:spinner];

Use below code right in the beginning of the function which leads to the activity

[NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:selfwithObject:nil];

Declare these two methods for starting and stoping the Animation

-(void)threadStartAnimating:(id)data
{
[spinner startAnimating];
}


-(void)threadStopAnimating:(id)data
{
[spinner stopAnimating];
}

Please give feedback as this code is working fine in my own project

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top