123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- /*
- * 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 "UIButton+WebCache.h"
- #import "objc/runtime.h"
- #import "UIView+WebCacheOperation.h"
- static char imageURLStorageKey;
- @implementation UIButton (WebCache)
- - (NSURL *)sd_currentImageURL {
- NSURL *url = self.imageURLStorage[@(self.state)];
- if (!url) {
- url = self.imageURLStorage[@(UIControlStateNormal)];
- }
- return url;
- }
- - (NSURL *)sd_imageURLForState:(UIControlState)state {
- return self.imageURLStorage[@(state)];
- }
- - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state {
- [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
- }
- - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder {
- [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
- }
- - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
- [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
- }
- - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state completed:(SDWebImageCompletionBlock)completedBlock {
- [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:completedBlock];
- }
- - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock {
- [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:completedBlock];
- }
- - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock {
- [self setImage:placeholder forState:state];
- [self sd_cancelImageLoadForState:state];
-
- if (!url) {
- [self.imageURLStorage removeObjectForKey:@(state)];
-
- dispatch_main_async_safe(^{
- NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
- if (completedBlock) {
- completedBlock(nil, error, SDImageCacheTypeNone, url);
- }
- });
-
- return;
- }
-
- self.imageURLStorage[@(state)] = url;
- __weak UIButton *wself = self;
- id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
- if (!wself) return;
- dispatch_main_sync_safe(^{
- __strong UIButton *sself = wself;
- if (!sself) return;
- if (image) {
- [sself setImage:image forState:state];
- }
- if (completedBlock && finished) {
- completedBlock(image, error, cacheType, url);
- }
- });
- }];
- [self sd_setImageLoadOperation:operation forState:state];
- }
- - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state {
- [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
- }
- - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder {
- [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
- }
- - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
- [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
- }
- - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state completed:(SDWebImageCompletionBlock)completedBlock {
- [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:completedBlock];
- }
- - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock {
- [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:completedBlock];
- }
- - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock {
- [self sd_cancelImageLoadForState:state];
- [self setBackgroundImage:placeholder forState:state];
- if (url) {
- __weak UIButton *wself = self;
- id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
- if (!wself) return;
- dispatch_main_sync_safe(^{
- __strong UIButton *sself = wself;
- if (!sself) return;
- if (image) {
- [sself setBackgroundImage:image forState:state];
- }
- if (completedBlock && finished) {
- completedBlock(image, error, cacheType, url);
- }
- });
- }];
- [self sd_setBackgroundImageLoadOperation:operation forState:state];
- } else {
- dispatch_main_async_safe(^{
- NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
- if (completedBlock) {
- completedBlock(nil, error, SDImageCacheTypeNone, url);
- }
- });
- }
- }
- - (void)sd_setImageLoadOperation:(id<SDWebImageOperation>)operation forState:(UIControlState)state {
- [self sd_setImageLoadOperation:operation forKey:[NSString stringWithFormat:@"UIButtonImageOperation%@", @(state)]];
- }
- - (void)sd_cancelImageLoadForState:(UIControlState)state {
- [self sd_cancelImageLoadOperationWithKey:[NSString stringWithFormat:@"UIButtonImageOperation%@", @(state)]];
- }
- - (void)sd_setBackgroundImageLoadOperation:(id<SDWebImageOperation>)operation forState:(UIControlState)state {
- [self sd_setImageLoadOperation:operation forKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]];
- }
- - (void)sd_cancelBackgroundImageLoadForState:(UIControlState)state {
- [self sd_cancelImageLoadOperationWithKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]];
- }
- - (NSMutableDictionary *)imageURLStorage {
- NSMutableDictionary *storage = objc_getAssociatedObject(self, &imageURLStorageKey);
- if (!storage)
- {
- storage = [NSMutableDictionary dictionary];
- objc_setAssociatedObject(self, &imageURLStorageKey, storage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- return storage;
- }
- @end
- @implementation UIButton (WebCacheDeprecated)
- - (NSURL *)currentImageURL {
- return [self sd_currentImageURL];
- }
- - (NSURL *)imageURLForState:(UIControlState)state {
- return [self sd_imageURLForState:state];
- }
- - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state {
- [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
- }
- - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder {
- [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
- }
- - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
- [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
- }
- - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state completed:(SDWebImageCompletedBlock)completedBlock {
- [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
- if (completedBlock) {
- completedBlock(image, error, cacheType);
- }
- }];
- }
- - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock {
- [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
- if (completedBlock) {
- completedBlock(image, error, cacheType);
- }
- }];
- }
- - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock {
- [self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:options completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
- if (completedBlock) {
- completedBlock(image, error, cacheType);
- }
- }];
- }
- - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state {
- [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
- }
- - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder {
- [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
- }
- - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
- [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
- }
- - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state completed:(SDWebImageCompletedBlock)completedBlock {
- [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
- if (completedBlock) {
- completedBlock(image, error, cacheType);
- }
- }];
- }
- - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock {
- [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
- if (completedBlock) {
- completedBlock(image, error, cacheType);
- }
- }];
- }
- - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock {
- [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:options completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
- if (completedBlock) {
- completedBlock(image, error, cacheType);
- }
- }];
- }
- - (void)cancelCurrentImageLoad {
- // in a backwards compatible manner, cancel for current state
- [self sd_cancelImageLoadForState:self.state];
- }
- - (void)cancelBackgroundImageLoadForState:(UIControlState)state {
- [self sd_cancelBackgroundImageLoadForState:state];
- }
- @end
|