107 lines
3.1 KiB
Objective-C
107 lines
3.1 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 * innerConfigedUrl;
|
|
|
|
@property (nonatomic, strong) NetImageConfig * config;
|
|
|
|
@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)initImageUrl:(NSString *)imageUrl {
|
|
_imageUrl = imageUrl;
|
|
_innerConfigedUrl = [UIImageConstant configUrl:_imageUrl type:self.config.imageType radius:self.config.radius];
|
|
}
|
|
|
|
- (void)setImageUrl:(NSString *)imageUrl {
|
|
[self initImageUrl:imageUrl];
|
|
UIImage * image = [[SDImageCache sharedImageCache] imageFromCacheForKey:_innerConfigedUrl];
|
|
if (image) {
|
|
self.image = image;
|
|
self.state = NetImageStateLoaded;
|
|
} else {
|
|
[self loadImage:nil];
|
|
}
|
|
}
|
|
|
|
- (void)loadImage:(LoadCompletion)completion {
|
|
self.state = NetImageStateLoading;
|
|
[self sd_setImageWithURL:[NSURL URLWithString:_innerConfigedUrl] placeholderImage:self.config.placeHolder options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
|
|
if (error) {
|
|
self.state = NetImageStateUnload;
|
|
} else {
|
|
self.state = NetImageStateLoaded;
|
|
if (completion) {
|
|
completion(image, imageURL);
|
|
};
|
|
}
|
|
}];
|
|
}
|
|
- (void)loadImage:(LoadCompletion)completion fail:(LoadFail)fail{
|
|
self.state = NetImageStateLoading;
|
|
[self sd_setImageWithURL:[NSURL URLWithString:_innerConfigedUrl] placeholderImage:self.config.placeHolder options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
|
|
if (error) {
|
|
self.state = NetImageStateUnload;
|
|
if (fail){
|
|
fail(error);
|
|
}
|
|
} else {
|
|
self.state = NetImageStateLoaded;
|
|
if (completion) {
|
|
completion(image, imageURL);
|
|
};
|
|
}
|
|
}];
|
|
}
|
|
- (void)loadImageWithUrl:(NSString * _Nonnull)imageUrl completion:(LoadCompletion)completion {
|
|
[self initImageUrl:imageUrl];
|
|
[self loadImage:completion];
|
|
}
|
|
- (void)loadImageWithUrl:(NSString * _Nonnull)imageUrl completion:(LoadCompletion)completion fail:(LoadFail)fail{
|
|
[self initImageUrl:imageUrl];
|
|
[self loadImage:completion fail:fail];
|
|
}
|
|
- (NetImageConfig *)config {
|
|
if (!_config) {
|
|
_config = [[NetImageConfig alloc] init];
|
|
}
|
|
return _config;
|
|
}
|
|
|
|
@end
|