使用AFNetworking SetImageWithURL显示UIActivityIndi​​cator

时间:2013-01-13 04:51:15

标签: ios objective-c afnetworking

我正在使用AFNetworking库来设置包含URL内容的图像。在以下示例中,topLeftImage是UIImageView的实例。

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

我想要做的是在下载图像时显示UIActivityIndi​​catorView。但是,我无法确定触发活动指示器的启动和停止的位置。以下是我的活动指标代码。

UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[topLeftImage addSubview:activityIndicator];
[activityIndicator startAnimating];

如何将上述代码链接到setImageWithURL方法,以便仅在下载图像时显示活动指示符?

3 个答案:

答案 0 :(得分:38)

UIImageView + AFNetworking.h类提供了一个setImageWithURL方法,允许您使用成功和失败块。一旦请求成功完成或失败,将执行此块。因此,您可以在请求之前启动动画,并使停止成功并使失败块。这是一个示例代码:

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];
 }];

答案 1 :(得分:2)

Swift中extension的简单UIImageView

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()
            })
        }
    }
}

答案 2 :(得分:1)

基于LuisEspinoza回答我做了这个小类我希望​​能有所帮助。如果视图没有图像,我只需创建并添加UIActivityIndicatorView

#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