Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking - Fade animation on image while scrolling UITableView

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.

like image 368
Chompas Avatar asked Dec 20 '12 14:12

Chompas


1 Answers

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];
like image 64
Konstantinos Avatar answered Nov 15 '22 04:11

Konstantinos