SDWebImage使用以及缓存和本地存储的清理

新QQ交流群:418536818,之前的群已满


本来一直用AFNetWorking进行网络请求的而且SDWebImage的功能前者也有,但是奈何缓存清理的问题还要自己解决,项目时间紧,所以图片的异步请求只好换用SDWebImage了

1.SDWebImage下载地址https://github.com/rs/SDWebImage.git

2.添加库这个不想多说MapKit和ImageIO不能少,因为SDWebImage依赖此库

3.AppDelegate中

#import "SDWebImage/SDImageCache.h"

添加存储路径

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    // Override point for customization after application launch.

    //添加一个通用的只读存储路径

    NSString * bundlePath = [[[NSBundlemainBundle]resourcePath]stringByAppendingPathComponent:@"CustomPathImages"];

    [[SDImageCachesharedImageCache]addReadOnlyCachePath:bundlePath];

    return YES;

}

4.

#import "SDWebImage/UIImageView+WebCache.h"

注册神马的貌似没有也没事

-(void)doRegister{

    

    [SDWebImageManager sharedManager].imageDownloader.username =@"httpwatch";

    [SDWebImageManager sharedManager].imageDownloader.password =@"httpwatch01";


}

5.设定图片存储顺序 

-(void)doOrder{

    [SDWebImageManager.sharedManager.imageDownloadersetValue:@"SDWebImage Demo"forHTTPHeaderField:@"AppName"];

    SDWebImageManager.sharedManager.imageDownloader.executionOrder =SDWebImageDownloaderLIFOExecutionOrder;

  }

6.异步下载图片

 [cell.imageView sd_setImageWithURL:[NSURLURLWithString:[self.arrayobjectAtIndex:indexPath.row]]

    placeholderImage:nil

   options:indexPath.row ==0 ?

    SDWebImageRefreshCached:0 ];

    7.清空缓存图片

- (IBAction)doClean:(id)sender {

    [SDWebImageManager.sharedManager.imageCacheclearMemory];

    [SDWebImageManager.sharedManager.imageCacheclearDisk];

      [self.tableViewreloadData];

}

貌似这个地方需要tableView reloadData一次才可以

--------------------------------------------------------------------------------------------------------------------------下面是.m文件全部

#import "ViewController.h"

#import "SDWebImage/UIImageView+WebCache.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray * array;

@property (weak, nonatomic)IBOutlet UITableView *tableView;

@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    [selfdoRegister];

    [self doArray];

    [self doOrder];

    self.tableView.dataSource =self;

    self.tableView.delegate =self;

    [self.tableViewregisterClass:[UITableViewCellclass] forCellReuseIdentifier:@"cell"];

}

-(void)doRegister{

    

    [SDWebImageManager sharedManager].imageDownloader.username =@"httpwatch";

    [SDWebImageManager sharedManager].imageDownloader.password =@"httpwatch01";


}

-(void)doArray{

    self.array = [NSArrayarrayWithObjects:

                @"http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.35786508303135633",    // requires HTTP auth, used to demo the NTLM auth

                @"http://assets.sbnation.com/assets/2512203/dogflops.gif",

                @"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp",

                @"http://www.ioncannon.net/wp-content/uploads/2011/06/test9.webp",

                @"http://static2.dmcdn.net/static/video/656/177/44771656:jpeg_preview_small.jpg?20120509154705",

                @"http://static2.dmcdn.net/static/video/629/228/44822926:jpeg_preview_small.jpg?20120509181018",

                @"http://static2.dmcdn.net/static/video/116/367/44763611:jpeg_preview_small.jpg?20120509101749",

                @"http://static2.dmcdn.net/static/video/340/086/44680043:jpeg_preview_small.jpg?20120509180118",

                @"http://static2.dmcdn.net/static/video/666/645/43546666:jpeg_preview_small.jpg?20120412153140",

                @"http://static2.dmcdn.net/static/video/771/577/44775177:jpeg_preview_small.jpg?20120509183230",

                @"http://static2.dmcdn.net/static/video/810/508/44805018:jpeg_preview_small.jpg?20120508125339",

                @"http://static2.dmcdn.net/static/video/152/008/44800251:jpeg_preview_small.jpg?20120508103336",

                @"http://static2.dmcdn.net/static/video/694/741/35147496:jpeg_preview_small.jpg?20120508111445",

                @"http://static2.dmcdn.net/static/video/988/667/44766889:jpeg_preview_small.jpg?20120508130425",

                @"http://static2.dmcdn.net/static/video/282/467/44764282:jpeg_preview_small.jpg?20120507130637",

                @"http://static2.dmcdn.net/static/video/754/657/44756457:jpeg_preview_small.jpg?20120507093012",

                @"http://static2.dmcdn.net/static/video/831/107/44701138:jpeg_preview_small.jpg?20120506133917",

                @"http://static2.dmcdn.net/static/video/411/057/44750114:jpeg_preview_small.jpg?20120507014914",

                @"http://static2.dmcdn.net/static/video/894/547/44745498:jpeg_preview_small.jpg?20120509183004",

                @"http://static2.dmcdn.net/static/video/082/947/44749280:jpeg_preview_small.jpg?20120507015022",

                @"http://static2.dmcdn.net/static/video/833/347/44743338:jpeg_preview_small.jpg?20120509183004",

                @"http://static2.dmcdn.net/static/video/683/666/44666386:jpeg_preview_small.jpg?20120505111208",

                @"http://static2.dmcdn.net/static/video/595/946/44649595:jpeg_preview_small.jpg?20120507194104",

                @"http://static2.dmcdn.net/static/video/984/935/44539489:jpeg_preview_small.jpg?20120501184650",

                @"http://static2.dmcdn.net/static/video/440/416/44614044:jpeg_preview_small.jpg?20120505174152",

                @"http://static2.dmcdn.net/static/video/561/977/20779165:jpeg_preview_small.jpg?20120423161805",

                @"http://static2.dmcdn.net/static/video/104/546/44645401:jpeg_preview_small.jpg?20120507185246",

                @"http://static2.dmcdn.net/static/video/671/636/44636176:jpeg_preview_small.jpg?20120504021339",

                @"http://static2.dmcdn.net/static/video/142/746/44647241:jpeg_preview_small.jpg?20120504104451",

                @"http://static2.dmcdn.net/static/video/776/860/44068677:jpeg_preview_small.jpg?20120507185251",

                @"http://static2.dmcdn.net/static/video/892/748/51847298:jpeg_preview_small.jpg?20121105224514",

                @"http://static2.dmcdn.net/static/video/640/048/51840046:jpeg_preview_small.jpg?20121105214430",

                @"http://static2.dmcdn.net/static/video/153/648/51846351:jpeg_preview_small.jpg?20121105214426",

                @"http://static2.dmcdn.net/static/video/769/248/51842967:jpeg_preview_small.jpg?20121105214255",

                @"http://static2.dmcdn.net/static/video/720/448/51844027:jpeg_preview_small.jpg?20121105214248",

                @"http://static2.dmcdn.net/static/video/895/048/51840598:jpeg_preview_small.jpg?20121105214234",

                @"http://static2.dmcdn.net/static/video/893/348/51843398:jpeg_preview_small.jpg?20121105214157",

                @"http://static2.dmcdn.net/static/video/351/748/51847153:jpeg_preview_small.jpg?20121105214106",

                @"http://static2.dmcdn.net/static/video/364/648/51846463:jpeg_preview_small.jpg?20121105215005",

                @"http://static2.dmcdn.net/static/video/269/938/51839962:jpeg_preview_small.jpg?20121105214014",

                nil];

}


-(void)doOrder{

    [SDWebImageManager.sharedManager.imageDownloadersetValue:@"SDWebImage Demo"forHTTPHeaderField:@"AppName"];

    SDWebImageManager.sharedManager.imageDownloader.executionOrder =SDWebImageDownloaderLIFOExecutionOrder;

  



}


- (IBAction)doClean:(id)sender {

    [SDWebImageManager.sharedManager.imageCacheclearMemory];

    [SDWebImageManager.sharedManager.imageCacheclearDisk];

      [self.tableViewreloadData];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{

    return self.array.count;



}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString * identifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

    [cell.imageView sd_setImageWithURL:[NSURLURLWithString:[self.arrayobjectAtIndex:indexPath.row]]

    placeholderImage:nil

   options:indexPath.row ==0 ?

    SDWebImageRefreshCached:0 ];

    

    return cell;




}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值