iOS中图片缓存策略

来源:互联网 发布:淘宝手机端详情页gif 编辑:程序博客网 时间:2024/05/18 02:38

在iOS开发中,经常遇到一个问题,就是如何缓存从网上下载好的图片。首先想到的办法是将图片保存在字典中。但是使用NSCache来保存的话,会更好。

NSCache于字典的不同之处在于,当系统资源耗尽时,NSCache可以自行删除缓存,而采用字典要自己编写挂钩。此外NSCache并不会拷贝键,而是采用保留,NSCache对象不拷贝键的原因在于,键都是由不支持拷贝的操作对象来充当的。最后NSCache是线程安全的。

新建一个图片缓存的类

#import <Foundation/Foundation.h>


@class FNSImageCache;


@protocol FNSImageCacheDelegate <NSObject>


@optional


- (void)successFetchData:(NSData *)data;

@end


@interface FNSImageCache : NSObject


@property (nonatomic,weak)id<FNSImageCacheDelegate> delegate;


-(void)fetchDataWithURL:(NSURL *)url;


@end


在.m文件中

#import "FNSImageCache.h"



@implementation FNSImageCache

{

    NSCache *_cache;

    

}


- (instancetype)init

{

    if (self = [superinit]) {

        _cache = [NSCachenew];

        

        _cache.countLimit =100;

        

        _cache.totalCostLimit =5 * 1024 * 1024;

    }

    return self;

}


- (void)fetchDataWithURL:(NSURL *)url

{

    NSData *cacheData = [_cacheobjectForKey:url];

    if (cacheData) {

        [self useData:cacheData];

    }

    else{

        [selfdownloadDataFromInternet:url];

    }

}


- (void)downloadDataFromInternet:(NSURL *)url{

    NSURLRequest *request = [NSURLRequestrequestWithURL:url];

    NSURLSession *session = [NSURLSessionsharedSession];

    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError * _Nullable error) {

        

        [_cache setObject:dataforKey:url cost:data.length];

        [self useData:data];

        

    }];

    [task resume];

    

}

- (void)useData:(NSData *)data{

    if ([self.delegaterespondsToSelector:@selector(successFetchData:)]) {

        [self.delegatesuccessFetchData:data];

    }

  

}


@end


控制器中

#import "ViewController.h"

#import "FNSImageCache.h"


@interface ViewController ()<FNSImageCacheDelegate>


@property (weak, nonatomic) IBOutletUIImageView *imageView;


- (IBAction)downloadImageClick:(id)sender;


@property (nonatomic,strong)FNSImageCache *cache;

@end


@implementation ViewController



- (void)viewDidLoad {

    [superviewDidLoad];

    

    

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)downloadImageClick:(id)sender {

    FNSImageCache *cache = [[FNSImageCachealloc] init];

    cache.delegate = self;

    NSURL *url = [NSURLURLWithString:@"https://gd2.alicdn.com/bao/uploaded/i2/TB1cXuXHFXXXXaxXVXXXXXXXXXX_!!0-item_pic.jpg"];

    [cache fetchDataWithURL:url];

    

}

//FNSImageCache的代理方法


- (void)successFetchData:(NSData *)data

{

    UIImage *image = [UIImageimageWithData:data];

    [self.imageViewsetImage:image];

    

}

@end


0 0