UIButton+AFNetworking.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // UIButton+AFNetworking.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. #import <Foundation/Foundation.h>
  22. #import <Availability.h>
  23. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  24. #import <UIKit/UIKit.h>
  25. NS_ASSUME_NONNULL_BEGIN
  26. @protocol AFURLResponseSerialization, AFImageCache;
  27. /**
  28. This category adds methods to the UIKit framework's `UIButton` class. The methods in this category provide support for loading remote images and background images asynchronously from a URL.
  29. @warning Compound values for control `state` (such as `UIControlStateHighlighted | UIControlStateDisabled`) are unsupported.
  30. */
  31. @interface UIButton (AFNetworking)
  32. ///----------------------------
  33. /// @name Accessing Image Cache
  34. ///----------------------------
  35. /**
  36. The image cache used to improve image loading performance on scroll views. By default, `UIButton` will use the `sharedImageCache` of `UIImageView`.
  37. */
  38. + (id <AFImageCache>)sharedImageCache;
  39. /**
  40. Set the cache used for image loading.
  41. @param imageCache The image cache.
  42. */
  43. + (void)setSharedImageCache:(__nullable id <AFImageCache>)imageCache;
  44. ///------------------------------------
  45. /// @name Accessing Response Serializer
  46. ///------------------------------------
  47. /**
  48. The response serializer used to create an image representation from the server response and response data. By default, this is an instance of `AFImageResponseSerializer`.
  49. @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
  50. */
  51. @property (nonatomic, strong) id <AFURLResponseSerialization> imageResponseSerializer;
  52. ///--------------------
  53. /// @name Setting Image
  54. ///--------------------
  55. /**
  56. Asynchronously downloads an image from the specified URL, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.
  57. 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.
  58. @param state The control state.
  59. @param url The URL used for the image request.
  60. */
  61. - (void)setImageForState:(UIControlState)state
  62. withURL:(NSURL *)url;
  63. /**
  64. Asynchronously downloads an image from the specified URL, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.
  65. 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.
  66. @param state The control state.
  67. @param url The URL used for the image request.
  68. @param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the button will not change its image until the image request finishes.
  69. */
  70. - (void)setImageForState:(UIControlState)state
  71. withURL:(NSURL *)url
  72. placeholderImage:(nullable UIImage *)placeholderImage;
  73. /**
  74. Asynchronously downloads an image from the specified URL request, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.
  75. 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.
  76. If a success block is specified, it is the responsibility of the block to set the image of the button before returning. If no success block is specified, the default behavior of setting the image with `setImage:forState:` is applied.
  77. @param state The control state.
  78. @param urlRequest The URL request used for the image request.
  79. @param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the button will not change its image until the image request finishes.
  80. @param success A block to be executed when the image request operation finishes successfully. This block has no return value and takes two arguments: the server response and the image. If the image was returned from cache, the response parameter will be `nil`.
  81. @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 a single argument: the error that occurred.
  82. */
  83. - (void)setImageForState:(UIControlState)state
  84. withURLRequest:(NSURLRequest *)urlRequest
  85. placeholderImage:(nullable UIImage *)placeholderImage
  86. success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * __nullable response, UIImage *image))success
  87. failure:(nullable void (^)(NSError *error))failure;
  88. ///-------------------------------
  89. /// @name Setting Background Image
  90. ///-------------------------------
  91. /**
  92. Asynchronously downloads an image from the specified URL, and sets it as the background image for the specified state once the request is finished. Any previous background image request for the receiver will be cancelled.
  93. If the background image is cached locally, the background image is set immediately, otherwise the specified placeholder background image will be set immediately, and then the remote background image will be set once the request is finished.
  94. @param state The control state.
  95. @param url The URL used for the background image request.
  96. */
  97. - (void)setBackgroundImageForState:(UIControlState)state
  98. withURL:(NSURL *)url;
  99. /**
  100. Asynchronously downloads an image from the specified URL, and sets it as the background image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.
  101. 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.
  102. @param state The control state.
  103. @param url The URL used for the background image request.
  104. @param placeholderImage The background image to be set initially, until the background image request finishes. If `nil`, the button will not change its background image until the background image request finishes.
  105. */
  106. - (void)setBackgroundImageForState:(UIControlState)state
  107. withURL:(NSURL *)url
  108. placeholderImage:(nullable UIImage *)placeholderImage;
  109. /**
  110. Asynchronously downloads an image from the specified URL request, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.
  111. 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.
  112. If a success block is specified, it is the responsibility of the block to set the image of the button before returning. If no success block is specified, the default behavior of setting the image with `setBackgroundImage:forState:` is applied.
  113. @param state The control state.
  114. @param urlRequest The URL request used for the image request.
  115. @param placeholderImage The background image to be set initially, until the background image request finishes. If `nil`, the button will not change its background image until the background image request finishes.
  116. @param success A block to be executed when the image request operation finishes successfully. This block has no return value and takes two arguments: the server response and the image. If the image was returned from cache, the response parameter will be `nil`.
  117. @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 a single argument: the error that occurred.
  118. */
  119. - (void)setBackgroundImageForState:(UIControlState)state
  120. withURLRequest:(NSURLRequest *)urlRequest
  121. placeholderImage:(nullable UIImage *)placeholderImage
  122. success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * __nullable response, UIImage *image))success
  123. failure:(nullable void (^)(NSError *error))failure;
  124. ///------------------------------
  125. /// @name Canceling Image Loading
  126. ///------------------------------
  127. /**
  128. Cancels any executing image operation for the specified control state of the receiver, if one exists.
  129. @param state The control state.
  130. */
  131. - (void)cancelImageRequestOperationForState:(UIControlState)state;
  132. /**
  133. Cancels any executing background image operation for the specified control state of the receiver, if one exists.
  134. @param state The control state.
  135. */
  136. - (void)cancelBackgroundImageRequestOperationForState:(UIControlState)state;
  137. @end
  138. NS_ASSUME_NONNULL_END
  139. #endif