How to show an activity indicator in SDWebImage

后端 未结 6 1005
一向
一向 2020-12-25 12:50

Currently i am integrating SDWebImage in my project by following below things

1)#import \"UIButton+WebCache.h\"

2)[button setImageWithURL:url placeholderImag

6条回答
  •  渐次进展
    2020-12-25 13:48

    The best way I have found to do this is to use the SDWebImageManager class. Your view controller or some other class will then need to conform to the SDWebImageManagerDelegate protocol.

    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    UIImage *cachedImage = [manager imageWithURL:url];
    
    if (cachedImage) {
        [button setImage:cachedImage];
        // stop or remove your UIActivityIndicatorView here
    }
    else {
        [manager downloadWithURL:url delegate:self];
    }
    

    Once the image has been downloaded the delegate method will be called:

    - (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image {
        [button setImage:image];
        // stop or remove your UIActivityIndicatorView here
    }
    

    There is also a delegate method for when an error occurs downloading an image

    - (void)webImageManager:(SDWebImageManager *)imageManager didFailWithError:(NSError *)error {
        // Handle error here
    }
    

    If you have more than one button you may have problems determining which image belongs to which button after the image has downloaded. In this case you may need to have a button subclass which handles the download as above and then updates its own image.

    Hope that helps.

提交回复
热议问题