SDWebImage常用方法

1、常用的方法

//常用方法一
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;

//例如:
 NSURL *url = [NSURL URLWithString:app.icon];
 UIImage *placeholder = [UIImage imageNamed:@"placeholder"];
[cell.imageView sd_setImageWithURL:url placeholderImage:placeholder];

//常用方法二
//参数三:SDWebImageOptions
//SDWebImageRetryFailed :下载失败后,会重新继续下载
//SDWebImageLowPriority :正在进行UI交互操作时,停止内部的下载操作
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;

//例如
SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageLowPriority;
[cell.imageView sd_setImageWithURL:url placeholderImage:placeholder options:options];


//常用方法三
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock;

//例如:
[cell.imageView sd_setImageWithURL:url placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    NSLog(@"----图片加载完毕---%@", image);
}];

//常用方法四
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock ;

//例如:
SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageLowPriority;
[cell.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、内存处理,当app收到内存警告时

//该方法写在AppDelegate.m文件中
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    SDWebImageManager *mgr = [SDWebImageManager sharedManager];
    
    // 1.取消正在下载的操作
    [mgr cancelAll];
    
    // 2.清除内存缓存
    [mgr.imageCache clearMemory];
}

你可能感兴趣的:(SDWebImage常用方法)