commit | author | age
|
6e1425
|
1 |
// UIImageView+AFNetworking.h |
H |
2 |
// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) |
|
3 |
// |
|
4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|
5 |
// of this software and associated documentation files (the "Software"), to deal |
|
6 |
// in the Software without restriction, including without limitation the rights |
|
7 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
8 |
// copies of the Software, and to permit persons to whom the Software is |
|
9 |
// furnished to do so, subject to the following conditions: |
|
10 |
// |
|
11 |
// The above copyright notice and this permission notice shall be included in |
|
12 |
// all copies or substantial portions of the Software. |
|
13 |
// |
|
14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
16 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
17 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
18 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
19 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
20 |
// THE SOFTWARE. |
|
21 |
|
|
22 |
#import <Foundation/Foundation.h> |
|
23 |
|
|
24 |
#import <Availability.h> |
|
25 |
|
|
26 |
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) |
|
27 |
|
|
28 |
#import <UIKit/UIKit.h> |
|
29 |
|
|
30 |
NS_ASSUME_NONNULL_BEGIN |
|
31 |
|
|
32 |
@protocol AFURLResponseSerialization, AFImageCache; |
|
33 |
|
|
34 |
/** |
|
35 |
This category adds methods to the UIKit framework's `UIImageView` class. The methods in this category provide support for loading remote images asynchronously from a URL. |
|
36 |
*/ |
|
37 |
@interface UIImageView (AFNetworking) |
|
38 |
|
|
39 |
///---------------------------- |
|
40 |
/// @name Accessing Image Cache |
|
41 |
///---------------------------- |
|
42 |
|
|
43 |
/** |
|
44 |
The image cache used to improve image loading performance on scroll views. By default, this is an `NSCache` subclass conforming to the `AFImageCache` protocol, which listens for notification warnings and evicts objects accordingly. |
|
45 |
*/ |
|
46 |
+ (id <AFImageCache>)sharedImageCache; |
|
47 |
|
|
48 |
/** |
|
49 |
Set the cache used for image loading. |
|
50 |
|
|
51 |
@param imageCache The image cache. |
|
52 |
*/ |
|
53 |
+ (void)setSharedImageCache:(__nullable id <AFImageCache>)imageCache; |
|
54 |
|
|
55 |
///------------------------------------ |
|
56 |
/// @name Accessing Response Serializer |
|
57 |
///------------------------------------ |
|
58 |
|
|
59 |
/** |
|
60 |
The response serializer used to create an image representation from the server response and response data. By default, this is an instance of `AFImageResponseSerializer`. |
|
61 |
|
|
62 |
@discussion Subclasses of `AFImageResponseSerializer` could be used to perform post-processing, such as color correction, face detection, or other effects. See https://github.com/AFNetworking/AFCoreImageSerializer |
|
63 |
*/ |
|
64 |
@property (nonatomic, strong) id <AFURLResponseSerialization> imageResponseSerializer; |
|
65 |
|
|
66 |
///-------------------- |
|
67 |
/// @name Setting Image |
|
68 |
///-------------------- |
|
69 |
|
|
70 |
/** |
|
71 |
Asynchronously downloads an image from the specified URL, and sets it once the request is finished. Any previous image request for the receiver will be cancelled. |
|
72 |
|
|
73 |
If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. |
|
74 |
|
|
75 |
By default, URL requests have a `Accept` header field value of "image / *", a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set not handle cookies. To configure URL requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:` |
|
76 |
|
|
77 |
@param url The URL used for the image request. |
|
78 |
*/ |
|
79 |
- (void)setImageWithURL:(NSURL *)url; |
|
80 |
|
|
81 |
/** |
|
82 |
Asynchronously downloads an image from the specified URL, and sets it once the request is finished. Any previous image request for the receiver will be cancelled. |
|
83 |
|
|
84 |
If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. |
|
85 |
|
|
86 |
By default, URL requests have a `Accept` header field value of "image / *", a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set not handle cookies. To configure URL requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:` |
|
87 |
|
|
88 |
@param url The URL used for the image request. |
|
89 |
@param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes. |
|
90 |
*/ |
|
91 |
- (void)setImageWithURL:(NSURL *)url |
|
92 |
placeholderImage:(nullable UIImage *)placeholderImage; |
|
93 |
|
|
94 |
/** |
|
95 |
Asynchronously downloads an image from the specified URL request, and sets it once the request is finished. Any previous image request for the receiver will be cancelled. |
|
96 |
|
|
97 |
If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. |
|
98 |
|
|
99 |
If a success block is specified, it is the responsibility of the block to set the image of the image view before returning. If no success block is specified, the default behavior of setting the image with `self.image = image` is applied. |
|
100 |
|
|
101 |
@param urlRequest The URL request used for the image request. |
|
102 |
@param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes. |
|
103 |
@param success A block to be executed when the image request operation finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the response parameter will be `nil`. |
|
104 |
@param failure A block object to be executed when the image request operation finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred. |
|
105 |
*/ |
|
106 |
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest |
|
107 |
placeholderImage:(nullable UIImage *)placeholderImage |
|
108 |
success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * __nullable response, UIImage *image))success |
|
109 |
failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * __nullable response, NSError *error))failure; |
|
110 |
|
|
111 |
/** |
|
112 |
Cancels any executing image operation for the receiver, if one exists. |
|
113 |
*/ |
|
114 |
- (void)cancelImageRequestOperation; |
|
115 |
|
|
116 |
@end |
|
117 |
|
|
118 |
#pragma mark - |
|
119 |
|
|
120 |
/** |
|
121 |
The `AFImageCache` protocol is adopted by an object used to cache images loaded by the AFNetworking category on `UIImageView`. |
|
122 |
*/ |
|
123 |
@protocol AFImageCache <NSObject> |
|
124 |
|
|
125 |
/** |
|
126 |
Returns a cached image for the specified request, if available. |
|
127 |
|
|
128 |
@param request The image request. |
|
129 |
|
|
130 |
@return The cached image. |
|
131 |
*/ |
|
132 |
- (nullable UIImage *)cachedImageForRequest:(NSURLRequest *)request; |
|
133 |
|
|
134 |
/** |
|
135 |
Caches a particular image for the specified request. |
|
136 |
|
|
137 |
@param image The image to cache. |
|
138 |
@param request The request to be used as a cache key. |
|
139 |
*/ |
|
140 |
- (void)cacheImage:(UIImage *)image |
|
141 |
forRequest:(NSURLRequest *)request; |
|
142 |
@end |
|
143 |
|
|
144 |
NS_ASSUME_NONNULL_END |
|
145 |
|
|
146 |
#endif |