AFNetworking - Fade animation on image while scrol

2019-03-25 20:38发布

问题:

I have a Table View which displays images using lazy loading from AFNetworking with a placeholder image. I'm trying to accomplish a way to fade from the placeholder to the image when it's loaded. My code here works, but if an image loads while scrolling the table, everything looks odd and the table stops scrolling. Same thing happens to a Horizontal scroller with the same effect.

Here's the code I'm using inside cellForRowAtIndexPath

    [cell.hotelImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:hotelImageUrl]]
                  placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                           success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *image ){

                               if (request) {
                                   //Fade animation
                                   [UIView transitionWithView:self.view
                                                     duration:1.0f
                                                      options:UIViewAnimationOptionTransitionCrossDissolve
                                                   animations:^{
                                                       [cell.hotelImage setImage:image];
                                                   } completion:NULL];

                               }


                           }
                           failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){

                           }
 ];

I'm using the if(request) to avoid the animation once the image is in the cache.

Many thanks in advance.

回答1:

instead of self.view try this cell.hotelImage this will work :)

 [UIView transitionWithView:cell.hotelImage
   duration:1.0f
   options:UIViewAnimationOptionTransitionCrossDissolve
   animations:^{[cell.hotelImage setImage:image];} 
 completion:NULL];