写这篇文章的原因,作为开发者也有好几年了,曾有对著名第三方SDWebImgae做一个简单的研究,今天就把当初研究写的Demo和原理分享给大家,面试曾遇到过哦~
一、创建一个继承NSObject的工具类DownImageManager
点h实现
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DownImageManager : NSObject
{
//创建一个内存字典
NSMutableDictionary * _memoryDict;
}
//创建一个单例类
+(DownImageManager *)shareManager;
//从内存中找
-(UIImage *)getImageFromMemoryWithUrl:(NSString *)urlStr;
//从沙盒中找
-(UIImage *)getImageFromSendBoxWithUrl:(NSString *)urlStr;
//网络下载
-(void)downImageFromNetWorkWithUrl:(NSString *)url imageView:(UIImageView *)imageView;
@end
点m实现
#import "NSString+Addtions.h"
#import "Base64.h"
DownImageManager * _downManager;
//NSOperationQueue * queue;
@implementation DownImageManager
#pragma mark -------只初始化一次,单例列
+(DownImageManager *)shareManager{
static dispatch_once_t once;
dispatch_once(&once, ^{
_downManager = [[DownImageManager alloc]init];
});
return _downManager;
}
//返回当前类的对象instancetype
-(instancetype)init{
self= [super init];
if (self) {
_memoryDict = [[NSMutableDictionary alloc ]init];
}
return self;
}
//存到内存
-(UIImage *)getImageFromMemoryWithUrl:(NSString *)urlStr{
UIImage *image = _memoryDict[urlStr];
return image;
}
//存到沙盒文件夹下
-(UIImage *)getImageFromSendBoxWithUrl:(NSString *)urlStr{
NSString * path = [NSString getFilePathWithDir:@"Documents/huangshiwen" file:[urlStr base64EncodedString]];
//从沙盒文件夹下找图片
UIImage *image= [UIImage imageWithContentsOfFile:path];
//如果有图片就存起来
if (image) {
[_memoryDict setObject:image forKey:urlStr];
}
return image;
}
//网络下载
-(void)downImageFromNetWorkWithUrl:(NSString *)url imageView:(UIImageView *)imageView{
NSDictionary * dict =@{@"url":url,@"imageView":imageView};
NSInvocationOperation * operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(loadImage:) object:dict];
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
//执行
[queue addOperation:operation];
}
-(void)loadImage:(NSDictionary *)dict{
//从字典找键值对(图片 网址 )
NSURL *url = [NSURL URLWithString:dict[@"url"]];
UIImageView *imageView = dict[@"imageView"];
//开始转换
NSData * data =[NSData dataWithContentsOfURL:url];
UIImage * image = [UIImage imageWithData:data];
//开始回到主线程刷新ui
dispatch_async(dispatch_get_main_queue(), ^{
//如果下载好了
if (image) {
//存入内存字典
[_memoryDict setObject:image forKey:dict[@"url"]];
//存入沙盒文件
NSString *path = [NSString getFilePathWithDir:@"Documents/huangshiwen" file:[dict[@"url"] base64EncodedString]];
//数据持久化存储
[data writeToFile:path atomically:YES];
//显示
imageView.image = image;
}
});
}
@end
二、新建一个UIImageView的category (UIImageView+Addtition.h)
#import <UIKit/UIKit.h>
@interface UIImageView (Addtition)
//扩展类方法
-(void)setImageWithUrl:(NSString *)urlStr;
//
-(void)setimageWithUrl:(NSString *)urlStr placeHolderImage:(UIImageView *)image;
@end
#import "UIImageView+Addtition.h"
#import "DownImageManager.h"
@implementation UIImageView (Addtition)
#pragma mark --------imageUrl
-(void)setImageWithUrl:(NSString *)urlStr{
DownImageManager * manager = [DownImageManager shareManager];
UIImage * image = [manager getImageFromMemoryWithUrl:urlStr];
//存在内存
if (image) {
self.image= image;
return;
}
//本地沙盒
image = [manager getImageFromSendBoxWithUrl:urlStr];
if (image) {
self.image=image;
return;
}
//加载网络
// self 当前调用这个方法的对象(当前类的对象)
[manager downImageFromNetWorkWithUrl:urlStr imageView:self];
}
#pragma mark ========占位图
-(void)setimageWithUrl:(NSString *)urlStr placeHolderImage:(UIImage *)image{
self.image =image;
[self setImageWithUrl:urlStr];
}
@end
三、使用
- (void)viewDidLoad {
[super viewDidLoad];
[self.imageView1 setImageWithUrl:@"http://video.rednet.cn/uploadfile/2015/0311/20150311111749232.jpg"];
[self.imageView2 setimageWithUrl:@"http://video.rednet.cn/uploadfile/2015/0311/20150311111749232.jpg" placeHolderImage:nil];
}
网友评论