From 4b00568942159142228e6e61c182c2d1964c2d33 Mon Sep 17 00:00:00 2001 From: Ali Servet Donmez Date: Mon, 25 Jul 2011 17:50:21 +0200 Subject: [PATCH] Added UIButton+WebCache category --- UIButton+WebCache.h | 18 ++++++++++++++++++ UIButton+WebCache.m | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 UIButton+WebCache.h create mode 100644 UIButton+WebCache.m diff --git a/UIButton+WebCache.h b/UIButton+WebCache.h new file mode 100644 index 00000000..7fc1c222 --- /dev/null +++ b/UIButton+WebCache.h @@ -0,0 +1,18 @@ +/* + * This file is part of the SDWebImage package. + * (c) Olivier Poitrey + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +#import "SDWebImageCompat.h" +#import "SDWebImageManagerDelegate.h" + +@interface UIButton (WebCache) + +- (void)setImageWithURL:(NSURL *)url; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder; +- (void)cancelCurrentImageLoad; + +@end diff --git a/UIButton+WebCache.m b/UIButton+WebCache.m new file mode 100644 index 00000000..ba7f9aca --- /dev/null +++ b/UIButton+WebCache.m @@ -0,0 +1,44 @@ +/* + * This file is part of the SDWebImage package. + * (c) Olivier Poitrey + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +#import "UIButton+WebCache.h" +#import "SDWebImageManager.h" + +@implementation UIButton (WebCache) + +- (void)setImageWithURL:(NSURL *)url +{ + [self setImageWithURL:url placeholderImage:nil]; +} + +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder +{ + SDWebImageManager *manager = [SDWebImageManager sharedManager]; + + // Remove in progress downloader from queue + [manager cancelForDelegate:self]; + + [self setImage:placeholder forState:UIControlStateNormal]; + + if (url) + { + [manager downloadWithURL:url delegate:self]; + } +} + +- (void)cancelCurrentImageLoad +{ + [[SDWebImageManager sharedManager] cancelForDelegate:self]; +} + +- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image +{ + [self setImage:image forState:UIControlStateNormal]; +} + +@end