项目中从服务器端下载图片这些几乎是必备的,使用时也很简单,只需引入SDWebImage文件
// // ViewController.m // sdWebImageDemo // // Created by City--Online on 15/6/15. // Copyright (c) 2015年 City--Online. All rights reserved. // #import "ViewController.h" #import "UIImageView+WebCache.h" @interface ViewController () @property(nonatomic,strong) UIImageView *imgView1; @property(nonatomic,strong) UIImageView *imgView2; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *claerBtn=[UIButton buttonWithType:UIButtonTypeSystem]; [claerBtn setTitle:@"清除缓存" forState:UIControlStateNormal]; claerBtn.frame=CGRectMake(20, 10, 100, 100); [claerBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; claerBtn.tag=10001; [self.view addSubview:claerBtn]; UIButton *loadBtn=[UIButton buttonWithType:UIButtonTypeSystem]; [loadBtn setTitle:@"加载" forState:UIControlStateNormal]; loadBtn.frame=CGRectMake(130, 10, 100, 100); [loadBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; loadBtn.tag=10002; [self.view addSubview:loadBtn]; _imgView1=[[UIImageView alloc]initWithFrame:CGRectMake(20, 120, 250, 250)]; _imgView1.backgroundColor=[UIColor grayColor]; [self.view addSubview:_imgView1]; _imgView2=[[UIImageView alloc]initWithFrame:CGRectMake(20, 380, 250, 250)]; _imgView2.backgroundColor=[UIColor grayColor]; [self.view addSubview:_imgView2]; } -(void)btnClick:(id)sender { UIButton *btn=(UIButton *)sender; if (btn.tag==10001) { [[SDImageCache sharedImageCache] clearDisk]; [[SDImageCache sharedImageCache] clearMemory]; } else if (btn.tag==10002) { NSURL *imgPath1=[NSURL URLWithString:@"http://s15.sinaimg.cn/middle/9914f9fdhbc6170891ebe&690"]; NSURL *imgPath2=[NSURL URLWithString:@"http://s14.sinaimg.cn/middle/9914f9fdhbc611c219f3d&690"]; //基本使用方法 // [_imgView1 sd_setImageWithURL:imgPath1 ]; // // //block // [_imgView2 sd_setImageWithURL:imgPath2 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { // NSLog(@"这里可以在图片加载完成之后做些事情"); // }]; //预先设定一张图片 // [_imgView1 sd_setImageWithURL:imgPath1 placeholderImage:[UIImage imageNamed:@"default.jpg"]]; // // //block 预先设定一张图片 // [_imgView2 sd_setImageWithURL:imgPath2 placeholderImage:[UIImage imageNamed:@"default.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { // NSLog(@"这里可以在图片加载完成之后做些事情"); // }]; // [_imgView1 sd_setImageWithURL:imgPath1 placeholderImage:[UIImage imageNamed:@"default.jpg"] options:SDWebImageCacheMemoryOnly]; SDWebImageManager *manager=[SDWebImageManager sharedManager]; [manager downloadImageWithURL:imgPath2 options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) { NSLog(@"%f",receivedSize/(float)expectedSize); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { NSLog(@"下载完成"); _imgView2.image=image; }]; // // /* // //失败后重试 // SDWebImageRetryFailed = 1 << 0, // // //UI交互期间开始下载,导致延迟下载比如UIScrollView减速。 // SDWebImageLowPriority = 1 << 1, // // //只进行内存缓存 // SDWebImageCacheMemoryOnly = 1 << 2, // // //这个标志可以渐进式下载,显示的图像是逐步在下载 // SDWebImageProgressiveDownload = 1 << 3, // // //刷新缓存 // SDWebImageRefreshCached = 1 << 4, // // //后台下载 // SDWebImageContinueInBackground = 1 << 5, // // //NSMutableURLRequest.HTTPShouldHandleCookies = YES; // // SDWebImageHandleCookies = 1 << 6, // // //允许使用无效的SSL证书 // //SDWebImageAllowInvalidSSLCertificates = 1 << 7, // // //优先下载 // SDWebImageHighPriority = 1 << 8, // // //延迟占位符 // SDWebImageDelayPlaceholder = 1 << 9, // // //改变动画形象 // SDWebImageTransformAnimatedImage = 1 << 10, // */ } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
时间: 2024-10-15 20:50:57