UIButton+AFNetworking.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // UIButton+AFNetworking.h
  2. // Copyright (c) 2011–2016 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 <TargetConditionals.h>
  23. #if TARGET_OS_IOS || TARGET_OS_TV
  24. #import <UIKit/UIKit.h>
  25. NS_ASSUME_NONNULL_BEGIN
  26. @class AFImageDownloader;
  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 the Image Downloader
  34. ///------------------------------------
  35. /**
  36. Set the shared image downloader used to download images.
  37. @param imageDownloader The shared image downloader used to download images.
  38. */
  39. + (void)setSharedImageDownloader:(AFImageDownloader *)imageDownloader;
  40. /**
  41. The shared image downloader used to download images.
  42. */
  43. + (AFImageDownloader *)sharedImageDownloader;
  44. ///--------------------
  45. /// @name Setting Image
  46. ///--------------------
  47. /**
  48. 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.
  49. 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.
  50. @param state The control state.
  51. @param url The URL used for the image request.
  52. */
  53. - (void)setImageForState:(UIControlState)state
  54. withURL:(NSURL *)url;
  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. @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.
  61. */
  62. - (void)setImageForState:(UIControlState)state
  63. withURL:(NSURL *)url
  64. placeholderImage:(nullable UIImage *)placeholderImage;
  65. /**
  66. 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.
  67. 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.
  68. 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.
  69. @param state The control state.
  70. @param urlRequest The URL request used for the image request.
  71. @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.
  72. @param success A block to be executed when the image data task 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`.
  73. @param failure A block object to be executed when the image data task 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.
  74. */
  75. - (void)setImageForState:(UIControlState)state
  76. withURLRequest:(NSURLRequest *)urlRequest
  77. placeholderImage:(nullable UIImage *)placeholderImage
  78. success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
  79. failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;
  80. ///-------------------------------
  81. /// @name Setting Background Image
  82. ///-------------------------------
  83. /**
  84. 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.
  85. 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.
  86. @param state The control state.
  87. @param url The URL used for the background image request.
  88. */
  89. - (void)setBackgroundImageForState:(UIControlState)state
  90. withURL:(NSURL *)url;
  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 image request for the receiver will be cancelled.
  93. 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.
  94. @param state The control state.
  95. @param url The URL used for the background image request.
  96. @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.
  97. */
  98. - (void)setBackgroundImageForState:(UIControlState)state
  99. withURL:(NSURL *)url
  100. placeholderImage:(nullable UIImage *)placeholderImage;
  101. /**
  102. 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.
  103. 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.
  104. 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.
  105. @param state The control state.
  106. @param urlRequest The URL request used for the image request.
  107. @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.
  108. @param success A block to be executed when the image data task 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`.
  109. @param failure A block object to be executed when the image data task 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.
  110. */
  111. - (void)setBackgroundImageForState:(UIControlState)state
  112. withURLRequest:(NSURLRequest *)urlRequest
  113. placeholderImage:(nullable UIImage *)placeholderImage
  114. success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
  115. failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;
  116. ///------------------------------
  117. /// @name Canceling Image Loading
  118. ///------------------------------
  119. /**
  120. Cancels any executing image task for the specified control state of the receiver, if one exists.
  121. @param state The control state.
  122. */
  123. - (void)cancelImageDownloadTaskForState:(UIControlState)state;
  124. /**
  125. Cancels any executing background image task for the specified control state of the receiver, if one exists.
  126. @param state The control state.
  127. */
  128. - (void)cancelBackgroundImageDownloadTaskForState:(UIControlState)state;
  129. @end
  130. NS_ASSUME_NONNULL_END
  131. #endif