SDWebImage(三方框架)
使用,当有内存警告的时候(SDWebImageManager)
#import "AppDelegate.h"
#import "SDWebImageManager.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[SDWebImageManager sharedManager].imageCache clearMemory];
[[SDWebImageManager sharedManager] cancelAll];
}
@end
UIImageView+WebCache
#import "ViewController.h"
#import "JXItem.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()
@property (nonatomic,strong) NSArray * items;
@property (nonatomic,strong) NSMutableDictionary * imageCache;
@property (nonatomic,strong) NSOperationQueue * queue;
@property (nonatomic,strong) NSMutableDictionary * operation;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - 懒加载
- (NSArray *)items {
if (_items == nil) {
_items = [[NSArray alloc] init];
NSString * path = [[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil];
NSArray * arrayDict = [NSArray arrayWithContentsOfFile:path];
NSMutableArray * items = [NSMutableArray array];
for (NSDictionary * dict in arrayDict) {
JXItem * item = [JXItem itemWithDict:dict];
[items addObject:item];
}
_items = items;
}
return _items;
}
- (NSMutableDictionary *)imageCache{
if (_imageCache == nil) {
_imageCache = [NSMutableDictionary dictionary];
}
return _imageCache;
}
- (NSOperationQueue *)queue{
if (_queue == nil) {
_queue = [[NSOperationQueue alloc] init];
_queue.maxConcurrentOperationCount = 3;
}
return _queue;
}
- (NSMutableDictionary *)operation{
if (_operation == nil) {
_operation = [NSMutableDictionary dictionary];
}
return _operation;
}
#pragma mark - UITableViewDelegate && DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * identifier = @"app";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
JXItem * item = self.items[indexPath.row];
cell.textLabel.text = item.name;
cell.detailTextLabel.text = item.download;
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:item.icon] placeholderImage:nil];
return cell;
}
@end
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:item.icon] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
NSLog(@"下载进度%.2f",1.0 * receivedSize / expectedSize);
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"下载完成");
}];
其中:options的参数
SDWebImageRetryFailed:表示下载失败后重新下载
SDWebImageLowPriority:低优先级,当在操作UI的时候先暂停下载
SDWebImageCacheMemoryOnly:只将图片下载到缓存中,不会保存到沙盒中
SDWebImageProgressiveDownload:像浏览器那样,下载一部分就显示一部分图片。默认是只有下载完成之后才会显示图片
SDWebImageRefreshCached:网络相关的吧,还没搞懂
SDWebImageContinueInBackground:后台下载,会发通知给苹果系统,当app处于后台的时候还会继续下载
SDWebImageHandleCookies:我靠还是不知道杂用啊
SDWebImageAllowInvalidSSLCertificates:麻蛋作者已经甩我N个银河系了
SDWebImageHighPriority:高优先级
SDWebImageDelayPlaceholder:就那大概啥意思吧,我连用都不会用这个啊
SDWebImageTransformAnimatedImage:绝望
SDWebImageAvoidAutoSetImage:我去见上帝了
[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:@"http://space.lamost.org/solar/earth/images/globe.jpg"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
NSLog(@"下载进度%.2f",1.0 * receivedSize / expectedSize);
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
NSLog(@"下载完成");
}];