-
Notifications
You must be signed in to change notification settings - Fork 1
/
UIImageView+RFImageWork.m
executable file
·107 lines (95 loc) · 2.33 KB
/
UIImageView+RFImageWork.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// UIImageView+RFImageWork.m
// RFApp
//
// Created by gouzhehua on 14-7-11.
// Copyright (c) 2014年 GZH. All rights reserved.
//
#import "UIImageView+RFImageWork.h"
#import "RFCmdConfig.h"
#import "RFKit.h"
#import "RFStorageKit.h"
#import <objc/runtime.h>
static void * kRFUIImageViewImageWorkKey = "kRFUIImageViewImageWorkKey";
@implementation UIImageView (RFImageWork)
- (void)setImageWithURL:(NSString *)url
{
[self setImageWithURL:url placeholderImage:nil];
}
- (void)setImageWithURL:(NSString *)url placeholderImage:(UIImage *)placeholderImage
{
[self setImageWithURLRequest:url placeholderImage:placeholderImage success:nil failure:nil downloading:nil];
}
- (void)setImageWithURLRequest:(NSString *)url
placeholderImage:(UIImage *)placeholderImage
success:(void (^)(RFImageWork *aWork))success
failure:(void (^)(RFImageWork *aWork))failure
downloading:(void (^)(RFImageWork *aWork))downloading
{
[self cancelImageWork];
if ([NSString isEmpty:url])
{
self.image = placeholderImage;
return;
}
else
{
RFCmdConfig *config = [RFCmdConfig defaultConfig];
NSString *cachePath = [config cachePathWithUrl:url args:nil];
if ([RFStorageKit isExist:cachePath])
{
self.image = SAFE_ARC_AUTORELEASE([[UIImage alloc] initWithContentsOfFile:cachePath]);
return;
}
else
{
self.image = placeholderImage;
}
}
__weak UIImageView *selfRef = self;
RFImageWork *work = [[RFImageWork alloc] initWithUrl:url];
work.successBlock = ^(RFImageWork *aWork)
{
selfRef.image = aWork.image;
[self setImageWork:nil];
if (success != nil)
{
success(aWork);
}
};
work.failedBlock = ^(RFImageWork *aWork)
{
[self setImageWork:nil];
if (failure != nil)
{
failure(aWork);
}
};
work.downloadingBlock = ^(RFImageWork *aWork)
{
if (downloading != nil)
{
downloading(aWork);
}
};
[self setImageWork:work];
[work startByOwner:nil];
}
- (RFImageWork *)imageWork
{
return (RFImageWork *)objc_getAssociatedObject(self, kRFUIImageViewImageWorkKey);
}
- (void)setImageWork:(RFImageWork *)imageWork
{
objc_setAssociatedObject(self, kRFUIImageViewImageWorkKey, imageWork, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)cancelImageWork
{
RFImageWork *work = [self imageWork];
if (work != nil)
{
[work cancel];
[self setImageWork:nil];
}
}
@end