2013-01-13 9 views
13

Używam biblioteki AFNetworking do ustawiania obrazów z zawartością adresów URL. W poniższym przykładzie topLeftImage jest instancją UIImageView.Pokaż narzędzie UIActivityIndicator z AFNetworking SetImageWithURL

[topLeftImage setImageWithURL:[NSURL URLWithString:imageURL]]; 

Co chcę zrobić, to pokazać UIActivityIndicatorView podczas pobierania obrazu. Nie mogę jednak określić, gdzie uruchomić i zatrzymać wskaźnik aktywności. Poniżej znajduje się mój kod dla wskaźnika aktywności.

Jak mogę połączyć powyższy kod z metodą setImageWithURL, aby wskaźnik aktywności pojawiał się tylko podczas pobierania obrazu?

Odpowiedz

37

Kategoria UIImageView + AFNetworking.h udostępnia metodę setImageWithURL, która umożliwia korzystanie z bloków success i failure. Bloki zostaną wykonane, gdy żądanie zakończy się sukcesem lub niepowodzeniem. Możesz więc rozpocząć animację tuż przed żądaniem i umieścić ją w blokach sukcesu i niepowodzeń. To jest przykładowy kod:

NSURL *imageURL = [NSURL URLWithString:@"http://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/402px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg"]; 
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageURL]; 
[_activityIndicator setHidden:NO]; 
[_activityIndicator startAnimating]; 
[_imageView setImageWithURLRequest:imageRequest 
        placeholderImage:nil 
          success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) 
{ 
    [_activityIndicator setHidden:YES]; 
    [_activityIndicator stopAnimating]; 
    _imageView.image = image; 
} 
          failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) 
{ 
    [_activityIndicator setHidden:YES]; 
    [_activityIndicator stopAnimating]; 
}]; 
+0

upload przykładowy kod na github. https://github.com/luisespinoza/ActivityIndicatorImageURL – LuisEspinoza

+0

wygląda dobrze dla mnie. – javiergov

+3

uważaj na cykle zatrzymania, gdy odwołujesz się do bloków –

1

Na podstawie odpowiedzi LuisEspinoza zrobiłem tę małą kategorię Mam nadzieję, że może pomóc. Po prostu utworzę i dodaję UIActivityIndicatorView, jeśli widok nie zawiera obrazu.

#import <UIImageView+AFNetworking.h> 
@implementation UIImageView (LDToolbox) 
- (void)loadFromUrl:(NSURL *)url errorImage:(UIImage *)errorImage { 


    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"]; 
    __weak UIImageView *weakSelf = self; // Always use weak in Blocks 

    [[self subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; // Remove previous that not stop 

    __block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] init]; 
    activityIndicator.frame = self.bounds; 
    [self addSubview:activityIndicator]; 
    [activityIndicator setHidden:NO]; 
    [activityIndicator startAnimating]; 


    void (^removeSpinner)(void) = ^{ // Desctuctor Block 
     if (activityIndicator) { 
      [activityIndicator setHidden:YES]; 
      [activityIndicator stopAnimating]; 
      [activityIndicator removeFromSuperview]; 
      activityIndicator = nil; 
     } 
    }; 

    [self setImageWithURLRequest:request placeholderImage:nil success: ^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
     removeSpinner(); 
     weakSelf.image = image; 
    } failure: ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
     removeSpinner(); 
     NSLog(@"failed loading [%@]: %@", url, error); 
     weakSelf.image = errorImage; 
    }]; 
}  
@end 
2

Proste extension dla UIImageView w Swift:

extension UIImageView { 

    func setImageWithString(string: String?) { 

     if let string = string, let url = NSURL(string: string) { 

      let activityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))) 
      activityIndicatorView.hidden = false 
      activityIndicatorView.color = UIColor.lightGrayColor() 

      addSubview(activityIndicatorView) 
      bringSubviewToFront(activityIndicatorView) 

      activityIndicatorView.startAnimating() 

      setImageWithURLRequest(NSURLRequest(URL: url), placeholderImage: nil, success: { request, response, image in 

       self.image = image 
       activityIndicatorView.hidden = true 
       activityIndicatorView.stopAnimating() 
       activityIndicatorView.removeFromSuperview() 

       }, failure: { request, response, error in 

        activityIndicatorView.hidden = true 
        activityIndicatorView.stopAnimating() 
        activityIndicatorView.removeFromSuperview() 
      }) 
     } 
    } 
}