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
#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
- iOS中图片缓存策略
- iOS中缓存图片
- iOS网络加载图片缓存策略之ASIDownloadCache缓存优化
- Android中图片的三级缓存策略
- Android中图片的三级缓存策略
- IOS开发网络加载图片缓存策略之──ASIDownloadCache缓存策略
- IOS开发网络加载图片缓存策略之──ASIDownloadCache缓存策略
- IOS开发网络加载图片缓存策略之──ASIDownloadCache缓存策略
- IOS开发网络加载图片缓存策略之──ASIDownloadCache缓存策略
- IOS开发网络加载图片缓存策略之──ASIDownloadCache缓存策略
- IOS开发网络加载图片缓存策略之──ASIDownloadCache缓存策略
- IOS开发网络加载图片缓存策略之──ASIDownloadCache缓存策略
- IOS开发网络加载图片缓存策略之──ASIDownloadCache缓存策略
- IOS开发网络加载图片缓存策略之──ASIDownloadCache缓存策略
- IOS开发网络加载图片缓存策略之──ASIDownloadCache缓存策略
- IOS开发网络加载图片缓存策略之──ASIDownloadCache缓存策略
- IOS开发网络加载图片缓存策略之──ASIDownloadCache缓存策略
- android 图片缓存策略
- 【bzoj2669】【cqoi2012】【局部极小值】【状压dp】
- 【C】题解 (五校联考3day2)
- Google C++编程规范
- 使用Spark MLlib来训练并服务于自然语言处理模型
- 二叉树中最大最小权值节点距离问题
- iOS中图片缓存策略
- NYOJ矩形嵌套
- chrome内核浏览器,排版错位
- [Android]自定义简易版日历控件
- hdu 5651
- Android 属性动画(Property Animation) 完全解析
- 机器学习ML简史
- JOptionPane类提示框的一些常用的方法
- ffmpeg: 视频转码命令例子