// // CKWebImage.h // UI高级2 // // Created by Jason_Msbaby on 15/10/12. // Copyright ? 2015年 张杰. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface CKWebImage : NSObject +(instancetype)defaultLoader; -(void)downLoader:(NSString *)path FinishHandle:(void(^)(UIImage *img))block; @end /** * UIImageView拓展类目 */ @interface UIImageView (CKWebImage) -(void)ck_setImageWithURL:(NSString *)url; -(void)ck_setImageWithURL:(NSString *)url placeholderImage:(UIImage *)hoderImage; @end
// // CKWebImage.m // UI高级2 // // Created by Jason_Msbaby on 15/10/12. // Copyright ? 2015年 张杰. All rights reserved. // #import "CKWebImage.h" #import <CommonCrypto/CommonCrypto.h> @interface CKWebImage () @property(nonatomic, strong) NSMutableArray *history;// The DownLoad file History @end @implementation CKWebImage static CKWebImage *handle; +(instancetype)defaultLoader{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ handle = [CKWebImage new]; }); return handle; } - (instancetype)init { self = [super init]; if (self) { self.history = [NSMutableArray array]; } return self; } -(void)downLoader:(NSString *)path FinishHandle:(void (^)(UIImage *))block{ if (!block || !path) { NSLog(@"参数有误"); return ; } NSString *imageMD5Name = [self parseMD5:path]; //get the cache path in the phone NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject; NSString *fileName = [cachePath stringByAppendingPathComponent:imageMD5Name]; if ([self.history containsObject:imageMD5Name]) { NSData *data = [NSData dataWithContentsOfFile:fileName]; UIImage *img = [UIImage imageWithData:data]; block(img); NSLog(@"本地"); }else{ NSURL *url = [NSURL URLWithString:path]; NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (error || !data) { NSLog(@"请求出错"); return ; } //download success [data writeToFile:fileName atomically:YES]; [self.history addObject:imageMD5Name]; NSLog(@"网络"); UIImage *img = [UIImage imageWithData:data]; // sleep(3); dispatch_sync(dispatch_get_main_queue(), ^{ block(img); }); }]; [dataTask resume]; } } //parse str to md5 and return md5encoding -(NSString*)parseMD5:(NSString *)str{ unsigned char result[16]; const char *sourceData = str.UTF8String; CC_MD5(sourceData,strlen(sourceData), result); NSMutableString *res = [NSMutableString string]; for (int i = 0; i < 16; i++) { [res appendString:[NSString stringWithFormat:@"%x",result[i]]]; } return res; } @end @implementation UIImageView (CKWebImage) -(void)ck_setImageWithURL:(NSString *)url{ [self ck_setImageWithURL:url placeholderImage:nil]; } -(void)ck_setImageWithURL:(NSString *)url placeholderImage:(UIImage *)hoderImage{ self.image = hoderImage; __weak UIImageView *weakImageView = self; [[CKWebImage defaultLoader]downLoader:url FinishHandle:^(UIImage *img) { weakImageView.image = img; }]; } @end
时间: 2024-10-09 10:16:40