diff --git a/SDWebImage.xcodeproj/project.pbxproj b/SDWebImage.xcodeproj/project.pbxproj index 71905688..4a249e47 100644 --- a/SDWebImage.xcodeproj/project.pbxproj +++ b/SDWebImage.xcodeproj/project.pbxproj @@ -193,6 +193,12 @@ A18A6CC8172DC28500419892 /* UIImage+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CC5172DC28500419892 /* UIImage+GIF.h */; }; A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CC6172DC28500419892 /* UIImage+GIF.m */; }; A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CC6172DC28500419892 /* UIImage+GIF.m */; }; + ABBE71A718C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = ABBE71A518C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h */; settings = {ATTRIBUTES = (Public, ); }; }; + ABBE71A818C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m in Sources */ = {isa = PBXBuildFile; fileRef = ABBE71A618C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m */; }; + ABBE71A918C43B5800B75E91 /* UIImageView+HighlightedWebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = ABBE71A518C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h */; settings = {ATTRIBUTES = (Public, ); }; }; + ABBE71AA18C43B5800B75E91 /* UIImageView+HighlightedWebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = ABBE71A518C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h */; settings = {ATTRIBUTES = (Public, ); }; }; + ABBE71AB18C43B6000B75E91 /* UIImageView+HighlightedWebCache.m in Sources */ = {isa = PBXBuildFile; fileRef = ABBE71A618C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m */; }; + ABBE71AC18C43B6000B75E91 /* UIImageView+HighlightedWebCache.m in Sources */ = {isa = PBXBuildFile; fileRef = ABBE71A618C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -283,6 +289,8 @@ 5DA509F3187B68E7002FEB5C /* random.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = random.h; path = Vendors/libwebp/src/utils/random.h; sourceTree = ""; }; A18A6CC5172DC28500419892 /* UIImage+GIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+GIF.h"; sourceTree = ""; }; A18A6CC6172DC28500419892 /* UIImage+GIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+GIF.m"; sourceTree = ""; }; + ABBE71A518C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImageView+HighlightedWebCache.h"; sourceTree = ""; }; + ABBE71A618C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImageView+HighlightedWebCache.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -367,6 +375,8 @@ 53922DA9148C562D0056699D /* Categories */ = { isa = PBXGroup; children = ( + ABBE71A518C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h */, + ABBE71A618C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m */, 5D5B9140188EE8DD006D06BD /* NSData+ImageContentType.h */, 5D5B9141188EE8DD006D06BD /* NSData+ImageContentType.m */, 53EDFB8817623F7C00698166 /* UIImage+MultiFormat.h */, @@ -511,6 +521,7 @@ 530E49EB16464C7F002868E7 /* SDWebImageDownloaderOperation.h in Headers */, A18A6CC8172DC28500419892 /* UIImage+GIF.h in Headers */, 53EDFB8B17623F7C00698166 /* UIImage+MultiFormat.h in Headers */, + ABBE71AA18C43B5800B75E91 /* UIImageView+HighlightedWebCache.h in Headers */, 53E481E017C300F6003E8957 /* alpha_processing.h in Headers */, 53EDFB941762547D00698166 /* UIImage+WebP.h in Headers */, 53EDFC9317625BE300698166 /* webpi.h in Headers */, @@ -546,6 +557,7 @@ 53761320155AD0D5005750A4 /* UIImageView+WebCache.h in Headers */, 530E49E816464C25002868E7 /* SDWebImageOperation.h in Headers */, 530E49EA16464C7C002868E7 /* SDWebImageDownloaderOperation.h in Headers */, + ABBE71A718C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h in Headers */, A18A6CC7172DC28500419892 /* UIImage+GIF.h in Headers */, 53EDFB8A17623F7C00698166 /* UIImage+MultiFormat.h in Headers */, ); @@ -568,6 +580,7 @@ 537D95AA17ECC1FE0097C263 /* UIImage+GIF.h in Headers */, 537D95AC17ECC1FE0097C263 /* UIImage+MultiFormat.h in Headers */, 537D95AD17ECC1FE0097C263 /* UIImage+WebP.h in Headers */, + ABBE71A918C43B5800B75E91 /* UIImageView+HighlightedWebCache.h in Headers */, 537D95AE17ECC1FE0097C263 /* webpi.h in Headers */, 537D95AF17ECC1FE0097C263 /* vp8li.h in Headers */, 537D95B017ECC1FE0097C263 /* vp8i.h in Headers */, @@ -735,6 +748,7 @@ A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */, 53EDFB8D17623F7C00698166 /* UIImage+MultiFormat.m in Sources */, 53EDFB961762547D00698166 /* UIImage+WebP.m in Sources */, + ABBE71AC18C43B6000B75E91 /* UIImageView+HighlightedWebCache.m in Sources */, 53EDFC9517625BE300698166 /* webp.c in Sources */, 53EDFC9D17625C1100698166 /* vp8l.c in Sources */, 53EDFCA117625C1100698166 /* vp8.c in Sources */, @@ -780,6 +794,7 @@ A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */, 5D5B9145188EE8DD006D06BD /* NSData+ImageContentType.m in Sources */, 53EDFB8C17623F7C00698166 /* UIImage+MultiFormat.m in Sources */, + ABBE71A818C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -805,6 +820,7 @@ 537D958417ECC1FE0097C263 /* vp8.c in Sources */, 5D5B9146188EE8DD006D06BD /* NSData+ImageContentType.m in Sources */, 537D958517ECC1FE0097C263 /* huffman.c in Sources */, + ABBE71AB18C43B6000B75E91 /* UIImageView+HighlightedWebCache.m in Sources */, 537D958617ECC1FE0097C263 /* frame.c in Sources */, 537D958717ECC1FE0097C263 /* alpha.c in Sources */, 537D958817ECC1FE0097C263 /* quant_levels_dec.c in Sources */, diff --git a/SDWebImage/UIImageView+HighlightedWebCache.h b/SDWebImage/UIImageView+HighlightedWebCache.h new file mode 100644 index 00000000..c82b8bf7 --- /dev/null +++ b/SDWebImage/UIImageView+HighlightedWebCache.h @@ -0,0 +1,78 @@ +/* + * 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 +#import "UIImageView+WebCache.h" + +/** + * Integrates SDWebImage async downloading and caching of remote images with UIImageView for highlighted state. + */ +@interface UIImageView (HighlightedWebCache) + +/** + * Set the imageView `highlightedImage` with an `url`. + * + * The downloand is asynchronous and cached. + * + * @param url The url for the image. + */ +- (void)setHighlightedImageWithURL:(NSURL *)url; +/** + * Set the imageView `highlightedImage` with an `url` and custom options. + * + * The downloand is asynchronous and cached. + * + * @param url The url for the image. + * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. + */ +- (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options; + +/** + * Set the imageView `highlightedImage` with an `url`. + * + * The downloand is asynchronous and cached. + * + * @param url The url for the image. + * @param completedBlock A block called when operation has been completed. This block as no return value + * and takes the requested UIImage as first parameter. In case of error the image parameter + * is nil and the second parameter may contain an NSError. The third parameter is a Boolean + * indicating if the image was retrived from the local cache of from the network. + */ +- (void)setHighlightedImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock; + +/** + * Set the imageView `highlightedImage` with an `url` and custom options. + * + * The downloand is asynchronous and cached. + * + * @param url The url for the image. + * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. + * @param completedBlock A block called when operation has been completed. This block as no return value + * and takes the requested UIImage as first parameter. In case of error the image parameter + * is nil and the second parameter may contain an NSError. The third parameter is a Boolean + * indicating if the image was retrived from the local cache of from the network. + */ +- (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock; + +/** + * Set the imageView `highlightedImage` with an `url` and custom options. + * + * The downloand is asynchronous and cached. + * + * @param url The url for the image. + * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. + * @param progressBlock A block called while image is downloading + * @param completedBlock A block called when operation has been completed. This block as no return value + * and takes the requested UIImage as first parameter. In case of error the image parameter + * is nil and the second parameter may contain an NSError. The third parameter is a Boolean + * indicating if the image was retrived from the local cache of from the network. + */ +- (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock; + + +@end diff --git a/SDWebImage/UIImageView+HighlightedWebCache.m b/SDWebImage/UIImageView+HighlightedWebCache.m new file mode 100644 index 00000000..d8b5586f --- /dev/null +++ b/SDWebImage/UIImageView+HighlightedWebCache.m @@ -0,0 +1,59 @@ +/* + * 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 "UIImageView+HighlightedWebCache.h" +#import "objc/runtime.h" + +static char operationKey; + +@implementation UIImageView (HighlightedWebCache) + +- (void)setHighlightedImageWithURL:(NSURL *)url { + [self setHighlightedImageWithURL:url options:0 progress:nil completed:nil]; +} + +- (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options { + [self setHighlightedImageWithURL:url options:options progress:nil completed:nil]; +} + +- (void)setHighlightedImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock { + [self setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock]; +} + +- (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { + [self setHighlightedImageWithURL:url options:options progress:nil completed:completedBlock]; +} + +- (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock { + [self cancelCurrentImageLoad]; + + if (url) { + __weak UIImageView *wself = self; + id operation = [SDWebImageManager.sharedManager downloadWithURL:url + options:options + progress:progressBlock + completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) + { + if (!wself) return; + dispatch_main_sync_safe (^ + { + if (!wself) return; + if (image) { + wself.highlightedImage = image; + [wself setNeedsLayout]; + } + if (completedBlock && finished) { + completedBlock(image, error, cacheType); + } + }); + }]; + objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + } +} + +@end