123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- /*
- * This file is part of the SDWebImage package.
- * (c) Olivier Poitrey <rs@dailymotion.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- #import "NSButton+WebCache.h"
- #if SD_MAC
- #import "objc/runtime.h"
- #import "UIView+WebCacheOperation.h"
- #import "UIView+WebCache.h"
- static inline NSString * imageOperationKey() {
- return @"NSButtonImageOperation";
- }
- static inline NSString * alternateImageOperationKey() {
- return @"NSButtonAlternateImageOperation";
- }
- @implementation NSButton (WebCache)
- #pragma mark - Image
- - (void)sd_setImageWithURL:(nullable NSURL *)url {
- [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
- }
- - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder {
- [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
- }
- - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {
- [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
- }
- - (void)sd_setImageWithURL:(nullable NSURL *)url completed:(nullable SDExternalCompletionBlock)completedBlock {
- [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
- }
- - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {
- [self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
- }
- - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock {
- [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
- }
- - (void)sd_setImageWithURL:(nullable NSURL *)url
- placeholderImage:(nullable UIImage *)placeholder
- options:(SDWebImageOptions)options
- progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
- completed:(nullable SDExternalCompletionBlock)completedBlock {
- self.sd_currentImageURL = url;
-
- __weak typeof(self)weakSelf = self;
- [self sd_internalSetImageWithURL:url
- placeholderImage:placeholder
- options:options
- operationKey:imageOperationKey()
- setImageBlock:^(NSImage * _Nullable image, NSData * _Nullable imageData) {
- weakSelf.image = image;
- }
- progress:progressBlock
- completed:completedBlock];
- }
- #pragma mark - Alternate Image
- - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url {
- [self sd_setAlternateImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
- }
- - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder {
- [self sd_setAlternateImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
- }
- - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {
- [self sd_setAlternateImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
- }
- - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url completed:(nullable SDExternalCompletionBlock)completedBlock {
- [self sd_setAlternateImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
- }
- - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {
- [self sd_setAlternateImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
- }
- - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock {
- [self sd_setAlternateImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
- }
- - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url
- placeholderImage:(nullable UIImage *)placeholder
- options:(SDWebImageOptions)options
- progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
- completed:(nullable SDExternalCompletionBlock)completedBlock {
- self.sd_currentAlternateImageURL = url;
-
- __weak typeof(self)weakSelf = self;
- [self sd_internalSetImageWithURL:url
- placeholderImage:placeholder
- options:options
- operationKey:alternateImageOperationKey()
- setImageBlock:^(NSImage * _Nullable image, NSData * _Nullable imageData) {
- weakSelf.alternateImage = image;
- }
- progress:progressBlock
- completed:completedBlock];
- }
- #pragma mark - Cancel
- - (void)sd_cancelCurrentImageLoad {
- [self sd_cancelImageLoadOperationWithKey:imageOperationKey()];
- }
- - (void)sd_cancelCurrentAlternateImageLoad {
- [self sd_cancelImageLoadOperationWithKey:alternateImageOperationKey()];
- }
- #pragma mar - Private
- - (NSURL *)sd_currentImageURL {
- return objc_getAssociatedObject(self, @selector(sd_currentImageURL));
- }
- - (void)setSd_currentImageURL:(NSURL *)sd_currentImageURL {
- objc_setAssociatedObject(self, @selector(sd_currentImageURL), sd_currentImageURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- - (NSURL *)sd_currentAlternateImageURL {
- return objc_getAssociatedObject(self, @selector(sd_currentAlternateImageURL));
- }
- - (void)setSd_currentAlternateImageURL:(NSURL *)sd_currentAlternateImageURL {
- objc_setAssociatedObject(self, @selector(sd_currentAlternateImageURL), sd_currentAlternateImageURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- @end
- #endif
|