NSButton+WebCache.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "NSButton+WebCache.h"
  9. #if SD_MAC
  10. #import "objc/runtime.h"
  11. #import "UIView+WebCacheOperation.h"
  12. #import "UIView+WebCache.h"
  13. static inline NSString * imageOperationKey() {
  14. return @"NSButtonImageOperation";
  15. }
  16. static inline NSString * alternateImageOperationKey() {
  17. return @"NSButtonAlternateImageOperation";
  18. }
  19. @implementation NSButton (WebCache)
  20. #pragma mark - Image
  21. - (void)sd_setImageWithURL:(nullable NSURL *)url {
  22. [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
  23. }
  24. - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder {
  25. [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
  26. }
  27. - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {
  28. [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
  29. }
  30. - (void)sd_setImageWithURL:(nullable NSURL *)url completed:(nullable SDExternalCompletionBlock)completedBlock {
  31. [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
  32. }
  33. - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {
  34. [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
  35. }
  36. - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock {
  37. [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
  38. }
  39. - (void)sd_setImageWithURL:(nullable NSURL *)url
  40. placeholderImage:(nullable UIImage *)placeholder
  41. options:(SDWebImageOptions)options
  42. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  43. completed:(nullable SDExternalCompletionBlock)completedBlock {
  44. self.sd_currentImageURL = url;
  45. __weak typeof(self)weakSelf = self;
  46. [self sd_internalSetImageWithURL:url
  47. placeholderImage:placeholder
  48. options:options
  49. operationKey:imageOperationKey()
  50. setImageBlock:^(NSImage * _Nullable image, NSData * _Nullable imageData) {
  51. weakSelf.image = image;
  52. }
  53. progress:progressBlock
  54. completed:completedBlock];
  55. }
  56. #pragma mark - Alternate Image
  57. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url {
  58. [self sd_setAlternateImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
  59. }
  60. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder {
  61. [self sd_setAlternateImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
  62. }
  63. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {
  64. [self sd_setAlternateImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
  65. }
  66. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url completed:(nullable SDExternalCompletionBlock)completedBlock {
  67. [self sd_setAlternateImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
  68. }
  69. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {
  70. [self sd_setAlternateImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
  71. }
  72. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock {
  73. [self sd_setAlternateImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
  74. }
  75. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url
  76. placeholderImage:(nullable UIImage *)placeholder
  77. options:(SDWebImageOptions)options
  78. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  79. completed:(nullable SDExternalCompletionBlock)completedBlock {
  80. self.sd_currentAlternateImageURL = url;
  81. __weak typeof(self)weakSelf = self;
  82. [self sd_internalSetImageWithURL:url
  83. placeholderImage:placeholder
  84. options:options
  85. operationKey:alternateImageOperationKey()
  86. setImageBlock:^(NSImage * _Nullable image, NSData * _Nullable imageData) {
  87. weakSelf.alternateImage = image;
  88. }
  89. progress:progressBlock
  90. completed:completedBlock];
  91. }
  92. #pragma mark - Cancel
  93. - (void)sd_cancelCurrentImageLoad {
  94. [self sd_cancelImageLoadOperationWithKey:imageOperationKey()];
  95. }
  96. - (void)sd_cancelCurrentAlternateImageLoad {
  97. [self sd_cancelImageLoadOperationWithKey:alternateImageOperationKey()];
  98. }
  99. #pragma mar - Private
  100. - (NSURL *)sd_currentImageURL {
  101. return objc_getAssociatedObject(self, @selector(sd_currentImageURL));
  102. }
  103. - (void)setSd_currentImageURL:(NSURL *)sd_currentImageURL {
  104. objc_setAssociatedObject(self, @selector(sd_currentImageURL), sd_currentImageURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  105. }
  106. - (NSURL *)sd_currentAlternateImageURL {
  107. return objc_getAssociatedObject(self, @selector(sd_currentAlternateImageURL));
  108. }
  109. - (void)setSd_currentAlternateImageURL:(NSURL *)sd_currentAlternateImageURL {
  110. objc_setAssociatedObject(self, @selector(sd_currentAlternateImageURL), sd_currentAlternateImageURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  111. }
  112. @end
  113. #endif