152 lines
4.3 KiB
Objective-C
152 lines
4.3 KiB
Objective-C
//
|
|
// NetImageView.m
|
|
// YUMI
|
|
//
|
|
// Created by zu on 2021/11/2.
|
|
//
|
|
|
|
#import "NetImageView.h"
|
|
#import <UIImageView+WebCache.h>
|
|
#import <SDImageCache.h>
|
|
|
|
@interface NetImageView()
|
|
|
|
@property (nonatomic, assign, readwrite) NetImageState state;
|
|
@property (nonatomic, copy) NSString * innerConfigUrl;
|
|
@property (nonatomic, strong) NetImageConfig * config;
|
|
@property (nonatomic, strong) UIImageView *lightImageView;
|
|
|
|
@end
|
|
|
|
@implementation NetImageView
|
|
|
|
- (instancetype)initWithUrl:(NSString *)url {
|
|
return [self initWithUrl:url config:nil];
|
|
}
|
|
|
|
- (instancetype)initWithConfig:(NetImageConfig *)config {
|
|
return [self initWithUrl:@"" config:config];
|
|
}
|
|
|
|
- (instancetype)initWithUrl:(NSString *)url config:(NetImageConfig *)config {
|
|
self = [super init];
|
|
if (self) {
|
|
_state = NetImageStateUnload;
|
|
_config = config;
|
|
if (_config.autoLoad) {
|
|
[self setImageUrl:url];
|
|
} else {
|
|
[self initImageUrl:url];
|
|
}
|
|
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)cancelLoadImage {
|
|
[self sd_cancelCurrentImageLoad];
|
|
}
|
|
|
|
- (void)initImageUrl:(NSString *)imageUrl {
|
|
_imageUrl = imageUrl;
|
|
_innerConfigUrl = [UIImageConstant configUrl:_imageUrl type:self.config.imageType radius:self.config.radius];
|
|
}
|
|
|
|
- (void)setBackgroundLightType:(NSInteger)backgroundLightType {
|
|
_backgroundLightType = backgroundLightType;
|
|
if (!_lightImageView) {
|
|
_lightImageView = [[UIImageView alloc] init];
|
|
[self insertSubview:_lightImageView atIndex:0];
|
|
[_lightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.mas_equalTo(self);
|
|
}];
|
|
}
|
|
switch (_backgroundLightType) {
|
|
case 1:
|
|
_lightImageView.image = [UIImage imageNamed:@"room_pk_result_avatar_bg_yellow"];
|
|
break;
|
|
case 2:
|
|
_lightImageView.image = [UIImage imageNamed:@"room_pk_result_avatar_bg_gray"];
|
|
break;
|
|
case 3:
|
|
_lightImageView.image = [UIImage imageNamed:@"room_pk_result_avatar_bg_purple"];
|
|
break;
|
|
default:
|
|
_lightImageView.image = nil;
|
|
break;
|
|
}
|
|
}
|
|
|
|
- (UIImage *)lightImage:(NSInteger)type {
|
|
switch (type) {
|
|
case 1:
|
|
return [UIImage imageNamed:@"room_pk_result_avatar_bg_yellow"];
|
|
case 2:
|
|
return [UIImage imageNamed:@"room_pk_result_avatar_bg_gray"];
|
|
case 3:
|
|
return [UIImage imageNamed:@"room_pk_result_avatar_bg_purple"];
|
|
default:
|
|
return nil;
|
|
}
|
|
}
|
|
|
|
- (void)setImageUrl:(NSString *)imageUrl {
|
|
[self initImageUrl:imageUrl];
|
|
[self loadImage:nil fail:nil];
|
|
}
|
|
|
|
- (void)loadImage:(LoadCompletion)completion {
|
|
[self loadImage:completion fail:nil];
|
|
}
|
|
|
|
- (void)loadImageWithUrl:(NSString *)imageUrl completion:(LoadCompletion)completion {
|
|
[self initImageUrl:imageUrl];
|
|
[self loadImage:completion fail:nil];
|
|
}
|
|
|
|
- (void)loadImageWithUrl:(NSString * _Nonnull)imageUrl completion:(LoadCompletion _Nullable)completion fail:(LoadFail _Nullable)fail{
|
|
[self initImageUrl:imageUrl];
|
|
[self loadImage:completion fail:fail];
|
|
}
|
|
|
|
- (void)loadImage:(LoadCompletion _Nullable)completion fail:(LoadFail _Nullable)fail{
|
|
self.state = NetImageStateLoading;
|
|
@kWeakify(self);
|
|
[self sd_setImageWithURL:[NSURL URLWithString:self.innerConfigUrl]
|
|
placeholderImage:self.config.placeHolder
|
|
options:SDWebImageRetryFailed | SDWebImageQueryMemoryData | SDWebImageQueryDiskDataSync
|
|
completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
|
|
@kStrongify(self);
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
if (error) {
|
|
self.state = NetImageStateUnload;
|
|
if (fail){
|
|
fail(error);
|
|
}
|
|
} else {
|
|
self.image = image;
|
|
self.state = NetImageStateLoaded;
|
|
if (completion) {
|
|
completion(image, imageURL);
|
|
};
|
|
}
|
|
});
|
|
}];
|
|
}
|
|
|
|
- (NetImageConfig *)config {
|
|
if (!_config) {
|
|
_config = [[NetImageConfig alloc] init];
|
|
}
|
|
return _config;
|
|
}
|
|
|
|
- (void)updateConfigPlaceHolder:(UIImage *)image {
|
|
self.config.placeHolder = image;
|
|
if (self.state == NetImageStateUnload) {
|
|
self.image = image;
|
|
}
|
|
}
|
|
|
|
@end
|