1

I have a collection view that is supposed to display a large collection of images from a server. I get all the image info from the server as one big JSON request, turn them into my own photo objects(POPhoto), then display them in the collection as shown below. The problem is if I scroll fast, some cell that had previously been displayed will come into view with their previous image set, then if I stop the scroll, I can watch the image suddenly change to something else.

It seems like AFNetworking is not cancelling the request when the cell moves off screen and reappears in a different position. So if I scroll to the middle of my photos, then back to the top, the cells at the top will first appear with the images they had originally, but will then switch to other photos that belong to the middle of the list.

My PhotoCell class:

-(void)setPhoto:(POPhoto *)p
{
    self->photo = p;

    p.delegate = self;
    self.progressView.hidden = YES;

    if (p.thumbnail != nil) {
        self.imageView.image = p.thumbnail;
    }
    else if (p.thumbnailURLPath != nil)
    {
        NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:p.thumbnailURLPath]];

        UIImage* placeholder = [UIImage imageWithContentsOfFile:@"placeholder.png"];
        __weak typeof(self) weakSelf = self;
        [self.imageView setImageWithURLRequest:request
                              placeholderImage:placeholder
                                       success:^(NSURLRequest* request, NSHTTPURLResponse* response, UIImage* image) {
                                           weakSelf.imageView.image = image;
                                           weakSelf.photo.thumbnail = image;
                                       }
                                       failure:nil];
    }
}

My collection view:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    POPhotoCell* photoCell = (POPhotoCell*)[self.collectionView dequeueReusableCellWithReuseIdentifier:kPhotoCellReuseIdentifier forIndexPath:indexPath];
    if (photoCell == nil) {
        NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"POPhotoCell" owner:self options:nil];
        photoCell = (POPhotoCell*)[topLevelObjects objectAtIndex:0];
    }
    photoCell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"polaroid1"]];
    [photoCell setPhoto:self.photos[indexPath.row]];
    return photoCell;
}
4

0 回答 0