這里面使用到了yykit庫(kù)行楞,首先在podfile里面添加庫(kù)
pod 'YYKit'
FGAsyncLoadImgUtil.h頭文件代碼很簡(jiǎn)單
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface FGAsyncLoadImgUtil : NSObject
+ (void)asyncLoadImgWithimgName:(NSString *)imgName block:(void(^)(UIImage *image))block;
@end
NS_ASSUME_NONNULL_END
FGAsyncLoadImgUtil.m文件蜂桶,主要邏輯是通過(guò)線程異步加載渲染并緩存至內(nèi)存來(lái)完成
#import "FGAsyncLoadImgUtil.h"
#import "YYKit.h"
@interface FGAsyncLoadImgUtil()
@property(nonatomic, strong) NSOperationQueue *operationQueue;
@property(nonatomic, strong) YYThreadSafeDictionary *safeImgDict;
@end
static FGAsyncLoadImgUtil *asyncLoadImgUtil = nil;
@implementation FGAsyncLoadImgUtil
+ (instancetype)shareInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
asyncLoadImgUtil = [[[self class] alloc] init];
});
return asyncLoadImgUtil;
}
- (instancetype)init
{
if (self = [super init])
{
// 根據(jù)測(cè)試數(shù)據(jù)以及項(xiàng)目線程控制,自行設(shè)置線程數(shù)量
NSInteger queueCount = 6;
self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.name = @"com.FGAsyncLoadImgUtil.www";
self.operationQueue.maxConcurrentOperationCount = queueCount;
self.safeImgDict = [YYThreadSafeDictionary dictionary];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning
{
[self.safeImgDict removeAllObjects];
}
+ (void)asyncLoadImgWithimgName:(NSString *)imgName block:(void(^)(UIImage *image))block
{
[[FGAsyncLoadImgUtil shareInstance] asyncLoadImgWithimgName:imgName block:block];
}
- (void)asyncLoadImgWithimgName:(NSString *)imgName block:(void(^)(UIImage *image))block
{
if (!imgName || imgName.length == 0)
{
return;
}
UIImage *image = [self.safeImgDict objectForKey:imgName];
if (image)
{
!block ? :block(image);
}
else
{
__weak typeof(self)wSelf = self;
[self.operationQueue addOperationWithBlock:^{
__strong typeof(wSelf) strongSelf = wSelf;
UIImage *image = [FGAsyncLoadImgUtil _getDrawImageByName:imgName];
if (image)
{
UIImage *storeImage = [strongSelf.safeImgDict objectForKey:imgName];
if (!storeImage)
{
[strongSelf.safeImgDict setObject:image forKey:imgName];
}
else
{
image = storeImage;
}
if (block)
{
dispatch_async(dispatch_get_main_queue(), ^{
block(image);
});
}
}
}];
}
}
+ (UIImage *)preDrawImageWithName:(NSString *)name
{
NSString *imgName = nil;
UIImage *img = nil;
if (!name || name.length == 0)
{
return img;
}
if (3 == [UIScreen mainScreen].scale)
{
imgName = [NSString stringWithFormat:@"%@@3x.png",name];
img = [FGAsyncLoadImgUtil _preDrawImageWithName:imgName];
}
if (img==nil)//沒(méi)有3x圖埃疫,可以嘗試獲取2x圖片
{
imgName = [NSString stringWithFormat:@"%@@2x.png",name];
img = [FGAsyncLoadImgUtil _preDrawImageWithName:imgName scale:2.0];
if (img == nil)
{
imgName = [NSString stringWithFormat:@"%@.jpeg",name];
img = [FGAsyncLoadImgUtil _preDrawImageWithName:imgName scale:1.0];
}
}
return img;
}
+ (UIImage *)_preDrawImageWithName:(NSString *)imgName
{
NSString *imgPath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:imgName];
NSData *data = [NSData dataWithContentsOfFile:imgPath];
if (data)
{
UIImage *image = [[UIImage alloc] initWithData:data scale:[UIScreen mainScreen].scale];
image = [image imageByDecoded];
return image;
}
else
{
return nil;
}
}
/**
根據(jù)指定的scale加載圖片
*/
+ (UIImage *)_preDrawImageWithName:(NSString *)imgName scale:(CGFloat)scale
{
NSString *imgPath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:imgName];
NSData *data = [NSData dataWithContentsOfFile:imgPath];
if (data)
{
UIImage *image = [[UIImage alloc] initWithData:data scale:scale];
image = [image imageByDecoded];
return image;
}
else
{
return nil;
}
}
+ (UIImage *)_getDrawImageByName:(NSString *)imgName
{
// NSString *imgPath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:imgName];
NSData *data = [NSData dataWithContentsOfFile:imgName];
if (data)
{
UIImage *image = [[UIImage alloc] initWithData:data scale:[UIScreen mainScreen].scale];
image = [image imageByDecoded];
return image;
}
else
{
return nil;
}
}
@end
最后是調(diào)用,調(diào)用很簡(jiǎn)單巩螃,在block回調(diào)里面設(shè)置圖片
[FGAsyncLoadImgUtil asyncLoadImgWithimgName:self.localPath block:^(UIImage * _Nonnull image) {
[self.backGroundImage setImage:image];
}];