SDWebImage(三方框架)

使用,当有内存警告的时候(SDWebImageManager)

#import "AppDelegate.h"
#import "SDWebImageManager.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


- (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;
    // sd_webImage的使用
    [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) {
        // receivedSize:已经总共接受到的字节数(一共接受的字节数)
        // 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) {

        // receivedSize:已经总共接受到的字节数(一共接受的字节数)
        // expectedSize:图片的总字节数
        NSLog(@"下载进度%.2f",1.0 * receivedSize / expectedSize);
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        NSLog(@"下载完成");
    }];

results matching ""

    No results matching ""