iOS SDWebImage较好的使用方法

1. 在需要用到网络图片的.m文件中

    NSURL *url = [NSURL URLWithString:imageUrlString];

  UIImage *placeholder = [UIImage imageNamed:@"placeholder"];

 

    // 前者:下载失败重试,后者:低优先级,如果tableView在滚动,则暂停下载,这样可以避免列表滚动时的因为下载图片而产生的卡顿

    SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageLowPriority;

 

    [self.imageView sd_setImageWithURL:url placeholderImage:placeholder options:options progress:^(NSInteger receivedSize, NSInteger expectedSize) { // 分段下载的原因,这个block可能会被调用多次

        NSLog(@"下载进度:%f", (double)receivedSize / expectedSize); // 这里可以监控下载的进度

    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        NSLog(@"----图片加载完毕---%@", image); // 在这里拿到图片,可以按功能模块划分,分文件夹存储,有助于开发调试。

    }];

2. 在appdelegate.m文件中

/**

 *  当app接收到内存警告,会调用此方法。

 */

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application

{

    SDWebImageManager *mgr = [SDWebImageManager sharedManager];

    

    // 1.取消正在下载的操作

    [mgr cancelAll];

    

    // 2.清除内存缓存

    [mgr.imageCache clearMemory];

}

原文地址:https://www.cnblogs.com/oumygade/p/4492310.html