From 1797c3c7cf4b3843bb8989f82685709cf52bee3a Mon Sep 17 00:00:00 2001 From: Homin Date: Thu, 22 Jun 2017 17:28:16 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A0=B9=E6=8D=AEtransfr?= =?UTF-8?q?om=E7=B1=BB=E5=9E=8B=E6=9D=A5=E5=BA=94=E5=AF=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Demo/YYWebImageDemo.xcodeproj/project.pbxproj | 6 + Demo/YYWebImageDemo/YYImageExample.m | 1 + Demo/YYWebImageDemo/YYTransformImageExample.h | 13 ++ Demo/YYWebImageDemo/YYTransformImageExample.m | 154 ++++++++++++++++++ YYWebImage/Categories/CALayer+YYWebImage.m | 4 +- .../Categories/MKAnnotationView+YYWebImage.m | 2 +- YYWebImage/Categories/UIButton+YYWebImage.m | 4 +- .../Categories/UIImageView+YYWebImage.h | 41 +++++ .../Categories/UIImageView+YYWebImage.m | 58 ++++++- YYWebImage/Categories/_YYWebImageSetter.h | 1 + YYWebImage/Categories/_YYWebImageSetter.m | 4 +- YYWebImage/YYWebImageManager.h | 2 + YYWebImage/YYWebImageManager.m | 18 +- YYWebImage/YYWebImageOperation.h | 2 + YYWebImage/YYWebImageOperation.m | 83 ++++++++-- 15 files changed, 361 insertions(+), 32 deletions(-) create mode 100644 Demo/YYWebImageDemo/YYTransformImageExample.h create mode 100644 Demo/YYWebImageDemo/YYTransformImageExample.m diff --git a/Demo/YYWebImageDemo.xcodeproj/project.pbxproj b/Demo/YYWebImageDemo.xcodeproj/project.pbxproj index 2a5e79e..d8aa331 100644 --- a/Demo/YYWebImageDemo.xcodeproj/project.pbxproj +++ b/Demo/YYWebImageDemo.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 197964151EFB9AEA002437BE /* YYTransformImageExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 197964141EFB9AEA002437BE /* YYTransformImageExample.m */; }; ABC4238E1BE3244A00703518 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = ABC4238D1BE3244A00703518 /* main.m */; }; ABC423911BE3244A00703518 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = ABC423901BE3244A00703518 /* AppDelegate.m */; }; ABC423941BE3244A00703518 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = ABC423931BE3244A00703518 /* ViewController.m */; }; @@ -66,6 +67,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 197964131EFB9AEA002437BE /* YYTransformImageExample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YYTransformImageExample.h; sourceTree = ""; }; + 197964141EFB9AEA002437BE /* YYTransformImageExample.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YYTransformImageExample.m; sourceTree = ""; }; ABC423891BE3244A00703518 /* YYWebImageDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = YYWebImageDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; ABC4238D1BE3244A00703518 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; ABC4238F1BE3244A00703518 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; @@ -211,6 +214,8 @@ ABC423FA1BE325EF00703518 /* YYWebImageExample.m */, ABC423FD1BE325EF00703518 /* YYImageExampleHelper.h */, ABC423FE1BE325EF00703518 /* YYImageExampleHelper.m */, + 197964131EFB9AEA002437BE /* YYTransformImageExample.h */, + 197964141EFB9AEA002437BE /* YYTransformImageExample.m */, ABC424091BE3264600703518 /* Resources */, ABC424081BE3262E00703518 /* Not available */, ABC4238C1BE3244A00703518 /* Supporting Files */, @@ -454,6 +459,7 @@ ABC423D31BE324F000703518 /* YYMemoryCache.m in Sources */, ABC4238E1BE3244A00703518 /* main.m in Sources */, ABC423DC1BE324F000703518 /* YYImage.m in Sources */, + 197964151EFB9AEA002437BE /* YYTransformImageExample.m in Sources */, ABC423E11BE324F000703518 /* YYWebImageOperation.m in Sources */, ABC424041BE325EF00703518 /* YYWebImageExample.m in Sources */, ABC423D41BE324F000703518 /* _YYWebImageSetter.m in Sources */, diff --git a/Demo/YYWebImageDemo/YYImageExample.m b/Demo/YYWebImageDemo/YYImageExample.m index 60e73d3..554b932 100644 --- a/Demo/YYWebImageDemo/YYImageExample.m +++ b/Demo/YYWebImageDemo/YYImageExample.m @@ -27,6 +27,7 @@ - (void)viewDidLoad { [self addCell:@"Animated Image" class:@"YYImageDisplayExample"]; [self addCell:@"Progressive Image" class:@"YYImageProgressiveExample"]; [self addCell:@"Web Image" class:@"YYWebImageExample"]; + [self addCell:@"Transform Image" class:@"YYTransformImageExample"]; //[self addCell:@"Benchmark" class:@"YYImageBenchmark"]; [self.tableView reloadData]; } diff --git a/Demo/YYWebImageDemo/YYTransformImageExample.h b/Demo/YYWebImageDemo/YYTransformImageExample.h new file mode 100644 index 0000000..02631ec --- /dev/null +++ b/Demo/YYWebImageDemo/YYTransformImageExample.h @@ -0,0 +1,13 @@ +// +// YYTransformImageExample.h +// YYWebImageDemo +// +// Created by xuemin on 2017/6/22. +// Copyright © 2017年 ibireme. All rights reserved. +// + +#import + +@interface YYTransformImageExample : UITableViewController + +@end diff --git a/Demo/YYWebImageDemo/YYTransformImageExample.m b/Demo/YYWebImageDemo/YYTransformImageExample.m new file mode 100644 index 0000000..4c08865 --- /dev/null +++ b/Demo/YYWebImageDemo/YYTransformImageExample.m @@ -0,0 +1,154 @@ +// +// YYTransformImageExample.m +// YYWebImageDemo +// +// Created by xuemin on 2017/6/22. +// Copyright © 2017年 ibireme. All rights reserved. +// + +#import "YYTransformImageExample.h" +#import "UIImageView+YYWebImage.h" +#import "UIImage+YYWebImage.h" +#import "YYImageCache.h" +#import "YYMemoryCache.h" +#import "YYDiskCache.h" + +@interface YYTransformImageExampleCell : UITableViewCell +@property (nonatomic, strong) UIImageView *avatar; +@end + +@implementation YYTransformImageExampleCell + +- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { + self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; + if (self) { + { + self.avatar = [[UIImageView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 100) / 2.0, 10, 100, 100)]; + [self.contentView addSubview:self.avatar]; + + } + + } + return self; +} + +@end + +@interface YYTransformImageExample () +@property (nonatomic, assign) NSInteger dataRows; +@property (nonatomic, strong) NSMutableArray *borderColors; +@end + +@implementation YYTransformImageExample + +- (NSMutableArray *)borderColors { + if (_borderColors == nil) { + _borderColors = [[NSMutableArray alloc] init]; + + [_borderColors addObject:[UIColor blueColor]]; + [_borderColors addObject:[UIColor orangeColor]]; + [_borderColors addObject:[UIColor redColor]]; + [_borderColors addObject:[UIColor yellowColor]]; + [_borderColors addObject:[UIColor purpleColor]]; + + } + return _borderColors; +} + +- (void)viewDidLoad { + [super viewDidLoad]; + + YYImageCache *cache = [YYWebImageManager sharedManager].cache; + // clear cache + [cache.memoryCache removeAllObjects]; + [cache.diskCache removeAllObjects]; + + _dataRows = 100; + + self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; + [self.tableView registerClass:[YYTransformImageExampleCell class] forCellReuseIdentifier:@"CellWithIdentifier"]; + + // Uncomment the following line to preserve selection between presentations. + // self.clearsSelectionOnViewWillAppear = NO; + + // Uncomment the following line to display an Edit button in the navigation bar for this view controller. + // self.navigationItem.rightBarButtonItem = self.editButtonItem; +} + +- (void)didReceiveMemoryWarning { + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. +} + +#pragma mark - Table view data source + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return 1; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + return _dataRows; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + YYTransformImageExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithIdentifier" forIndexPath:indexPath]; + NSInteger type = arc4random() % 5; + + UIColor *color = self.borderColors[type]; + + [cell.avatar yy_setImageWithURL:[NSURL URLWithString:@"https://d13yacurqjgara.cloudfront.net/users/26059/screenshots/1466318/getaway.jpg"] placeholder:[UIImage yy_imageWithColor:[UIColor lightGrayColor]] options:0 progress:nil transformType:type transform:^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) { + + return [image yy_imageByRoundCornerRadius:50 borderWidth:10 borderColor:color]; + } completion:nil]; + return cell; +} + +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { + return 120.5; +} + +/* +// Override to support conditional editing of the table view. +- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { + // Return NO if you do not want the specified item to be editable. + return YES; +} +*/ + +/* +// Override to support editing the table view. +- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { + if (editingStyle == UITableViewCellEditingStyleDelete) { + // Delete the row from the data source + [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; + } else if (editingStyle == UITableViewCellEditingStyleInsert) { + // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view + } +} +*/ + +/* +// Override to support rearranging the table view. +- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { +} +*/ + +/* +// Override to support conditional rearranging of the table view. +- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { + // Return NO if you do not want the item to be re-orderable. + return YES; +} +*/ + +/* +#pragma mark - Navigation + +// In a storyboard-based application, you will often want to do a little preparation before navigation +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { + // Get the new view controller using [segue destinationViewController]. + // Pass the selected object to the new view controller. +} +*/ + +@end diff --git a/YYWebImage/Categories/CALayer+YYWebImage.m b/YYWebImage/Categories/CALayer+YYWebImage.m index 8309a0c..53d9b65 100644 --- a/YYWebImage/Categories/CALayer+YYWebImage.m +++ b/YYWebImage/Categories/CALayer+YYWebImage.m @@ -172,8 +172,8 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL } }); }; - - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion]; +#warning 这里没有修改类型 + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformType:0 transform:transform completion:_completion]; weakSetter = setter; }); diff --git a/YYWebImage/Categories/MKAnnotationView+YYWebImage.m b/YYWebImage/Categories/MKAnnotationView+YYWebImage.m index d9afff4..50a1537 100644 --- a/YYWebImage/Categories/MKAnnotationView+YYWebImage.m +++ b/YYWebImage/Categories/MKAnnotationView+YYWebImage.m @@ -173,7 +173,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformType:0 transform:transform completion:_completion]; weakSetter = setter; }); }); diff --git a/YYWebImage/Categories/UIButton+YYWebImage.m b/YYWebImage/Categories/UIButton+YYWebImage.m index 8cc21d5..cb513f8 100644 --- a/YYWebImage/Categories/UIButton+YYWebImage.m +++ b/YYWebImage/Categories/UIButton+YYWebImage.m @@ -153,7 +153,7 @@ - (void)_yy_setImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformType:0 transform:transform completion:_completion]; weakSetter = setter; }); }); @@ -334,7 +334,7 @@ - (void)_yy_setBackgroundImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformType:0 transform:transform completion:_completion]; weakSetter = setter; }); }); diff --git a/YYWebImage/Categories/UIImageView+YYWebImage.h b/YYWebImage/Categories/UIImageView+YYWebImage.h index 6a66233..427d4bf 100644 --- a/YYWebImage/Categories/UIImageView+YYWebImage.h +++ b/YYWebImage/Categories/UIImageView+YYWebImage.h @@ -81,6 +81,46 @@ NS_ASSUME_NONNULL_BEGIN transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; +/** + Set the view's `image` with a specified URL. + + @param imageURL The image url (remote or local file path). + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param progress The block invoked (on main thread) during image request. + @param transform The block invoked (on background thread) to do additional image process. + @param completion The block invoked (on main thread) when image request completed. + */ +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + progress:(nullable YYWebImageProgressBlock)progress + transformType:(NSUInteger)transformType + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion; + + + +/** + Set the view's `image` with a specified URL. + + @param imageURL The image url (remote or local file path). + @param placeholder he image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param manager The manager to create image request operation. + @param progress The block invoked (on main thread) during image request. + @param transform The block invoked (on background thread) to do additional image process. + @param completion The block invoked (on main thread) when image request completed. + */ +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + manager:(nullable YYWebImageManager *)manager + progress:(nullable YYWebImageProgressBlock)progress + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion; + + /** Set the view's `image` with a specified URL. @@ -97,6 +137,7 @@ NS_ASSUME_NONNULL_BEGIN options:(YYWebImageOptions)options manager:(nullable YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress + transformType:(NSUInteger)transformType transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; diff --git a/YYWebImage/Categories/UIImageView+YYWebImage.m b/YYWebImage/Categories/UIImageView+YYWebImage.m index 1719a12..8dc8b16 100644 --- a/YYWebImage/Categories/UIImageView+YYWebImage.m +++ b/YYWebImage/Categories/UIImageView+YYWebImage.m @@ -89,11 +89,46 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL completion:completion]; } +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + progress:(nullable YYWebImageProgressBlock)progress + transformType:(NSUInteger)transformType + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion { + [self yy_setImageWithURL:imageURL + placeholder:placeholder + options:options + manager:nil + progress:progress + transformType:transformType + transform:transform + completion:completion]; +} + +- (void)yy_setImageWithURL:(NSURL *)imageURL + placeholder:(UIImage *)placeholder + options:(YYWebImageOptions)options + manager:(YYWebImageManager *)manager + progress:(YYWebImageProgressBlock)progress + transform:(YYWebImageTransformBlock)transform + completion:(YYWebImageCompletionBlock)completion { + + [self yy_setImageWithURL:imageURL + placeholder:placeholder + options:options + manager:manager + progress:progress + transformType:0 + transform:transform + completion:completion]; +} - (void)yy_setImageWithURL:(NSURL *)imageURL placeholder:(UIImage *)placeholder options:(YYWebImageOptions)options manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress + transformType:(NSUInteger)transformType transform:(YYWebImageTransformBlock)transform completion:(YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; @@ -126,7 +161,21 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL if (manager.cache && !(options & YYWebImageOptionUseNSURLCache) && !(options & YYWebImageOptionRefreshImageCache)) { - imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + if (transform) { + NSString *suffix = [NSString stringWithFormat:@"_%@", @(transformType)]; + NSString *cacheKey = [[manager cacheKeyForURL:imageURL] stringByAppendingString:suffix]; + imageFromMemory = [manager.cache getImageForKey:cacheKey withType:YYImageCacheTypeMemory]; +// if (imageFromMemory == nil) { +// imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; +// if (imageFromMemory) { +// imageFromMemory = transform(imageFromMemory, imageURL); +// [manager.cache setImage:imageFromMemory forKey:cacheKey]; +// } +// } + } + else { + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + } } if (imageFromMemory) { if (!(options & YYWebImageOptionAvoidSetImage)) { @@ -165,6 +214,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL transition.type = kCATransitionFade; [self.layer addAnimation:transition forKey:_YYWebImageFadeAnimationKey]; } + self.image = image; } if (completion) { @@ -177,7 +227,8 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformType:transformType transform:transform completion:_completion]; + weakSetter = setter; }); }); @@ -341,7 +392,8 @@ - (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion]; +#warning 这里没有修改类型 + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformType:0 transform:transform completion:_completion]; weakSetter = setter; }); }); diff --git a/YYWebImage/Categories/_YYWebImageSetter.h b/YYWebImage/Categories/_YYWebImageSetter.h index cf5a118..1dd4bc5 100644 --- a/YYWebImage/Categories/_YYWebImageSetter.h +++ b/YYWebImage/Categories/_YYWebImageSetter.h @@ -50,6 +50,7 @@ extern const NSTimeInterval _YYWebImageProgressiveFadeTime; options:(YYWebImageOptions)options manager:(YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress + transformType:(NSUInteger)transformType transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; diff --git a/YYWebImage/Categories/_YYWebImageSetter.m b/YYWebImage/Categories/_YYWebImageSetter.m index ec3c99b..f3253c1 100644 --- a/YYWebImage/Categories/_YYWebImageSetter.m +++ b/YYWebImage/Categories/_YYWebImageSetter.m @@ -48,6 +48,7 @@ - (int32_t)setOperationWithSentinel:(int32_t)sentinel options:(YYWebImageOptions)options manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress + transformType:(NSUInteger)transformType transform:(YYWebImageTransformBlock)transform completion:(YYWebImageCompletionBlock)completion { if (sentinel != _sentinel) { @@ -55,7 +56,8 @@ - (int32_t)setOperationWithSentinel:(int32_t)sentinel return _sentinel; } - NSOperation *operation = [manager requestImageWithURL:imageURL options:options progress:progress transform:transform completion:completion]; + NSOperation *operation = [manager requestImageWithURL:imageURL options:options progress:progress transformType:transformType transform:transform completion:completion]; + if (!operation && completion) { NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : @"YYWebImageOperation create failed." }; completion(nil, imageURL, YYWebImageFromNone, YYWebImageStageFinished, [NSError errorWithDomain:@"com.ibireme.webimage" code:-1 userInfo:userInfo]); diff --git a/YYWebImage/YYWebImageManager.h b/YYWebImage/YYWebImageManager.h index 6947faf..2b01ac8 100644 --- a/YYWebImage/YYWebImageManager.h +++ b/YYWebImage/YYWebImageManager.h @@ -191,6 +191,7 @@ typedef void (^YYWebImageCompletionBlock)(UIImage * _Nullable image, @param url The image url (remote or local file path). @param options The options to control image operation. @param progress Progress block which will be invoked on background thread (pass nil to avoid). + @param transformType The type of transform images after the cache @param transform Transform block which will be invoked on background thread (pass nil to avoid). @param completion Completion block which will be invoked on background thread (pass nil to avoid). @return A new image operation. @@ -198,6 +199,7 @@ typedef void (^YYWebImageCompletionBlock)(UIImage * _Nullable image, - (nullable YYWebImageOperation *)requestImageWithURL:(NSURL *)url options:(YYWebImageOptions)options progress:(nullable YYWebImageProgressBlock)progress + transformType:(NSUInteger)transformType transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; diff --git a/YYWebImage/YYWebImageManager.m b/YYWebImage/YYWebImageManager.m index 784fd8b..5546ae6 100644 --- a/YYWebImage/YYWebImageManager.m +++ b/YYWebImage/YYWebImageManager.m @@ -76,11 +76,13 @@ - (instancetype)initWithCache:(YYImageCache *)cache queue:(NSOperationQueue *)qu return self; } -- (YYWebImageOperation *)requestImageWithURL:(NSURL *)url - options:(YYWebImageOptions)options - progress:(YYWebImageProgressBlock)progress - transform:(YYWebImageTransformBlock)transform - completion:(YYWebImageCompletionBlock)completion { + +- (nullable YYWebImageOperation *)requestImageWithURL:(NSURL *)url + options:(YYWebImageOptions)options + progress:(nullable YYWebImageProgressBlock)progress + transformType:(NSUInteger)transformType + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.timeoutInterval = _timeout; @@ -88,16 +90,16 @@ - (YYWebImageOperation *)requestImageWithURL:(NSURL *)url request.allHTTPHeaderFields = [self headersForURL:url]; request.HTTPShouldUsePipelining = YES; request.cachePolicy = (options & YYWebImageOptionUseNSURLCache) ? - NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData; + NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData; YYWebImageOperation *operation = [[YYWebImageOperation alloc] initWithRequest:request options:options cache:_cache cacheKey:[self cacheKeyForURL:url] progress:progress - transform:transform ? transform : _sharedTransformBlock + transformType:transformType transform:transform ? transform : _sharedTransformBlock completion:completion]; - + if (_username && _password) { operation.credential = [NSURLCredential credentialWithUser:_username password:_password persistence:NSURLCredentialPersistenceForSession]; } diff --git a/YYWebImage/YYWebImageOperation.h b/YYWebImage/YYWebImageOperation.h index d15eb3b..4d96ae8 100644 --- a/YYWebImage/YYWebImageOperation.h +++ b/YYWebImage/YYWebImageOperation.h @@ -43,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nullable, nonatomic, strong, readonly) NSURLResponse *response; ///< The response for request. @property (nullable, nonatomic, strong, readonly) YYImageCache *cache; ///< The image cache. @property (nonatomic, strong, readonly) NSString *cacheKey; ///< The image cache key. +@property (nonatomic, strong, readonly) NSString *transformCacheKey; ///< The transform image cache key. @property (nonatomic, readonly) YYWebImageOptions options; ///< The operation's option. /** @@ -86,6 +87,7 @@ NS_ASSUME_NONNULL_BEGIN cache:(nullable YYImageCache *)cache cacheKey:(nullable NSString *)cacheKey progress:(nullable YYWebImageProgressBlock)progress + transformType:(NSUInteger)transformType transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion NS_DESIGNATED_INITIALIZER; diff --git a/YYWebImage/YYWebImageOperation.m b/YYWebImage/YYWebImageOperation.m index 118dd6d..b25b75d 100644 --- a/YYWebImage/YYWebImageOperation.m +++ b/YYWebImage/YYWebImageOperation.m @@ -241,16 +241,17 @@ + (dispatch_queue_t)_imageQueue { - (instancetype)init { @throw [NSException exceptionWithName:@"YYWebImageOperation init error" reason:@"YYWebImageOperation must be initialized with a request. Use the designated initializer to init." userInfo:nil]; - return [self initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]] options:0 cache:nil cacheKey:nil progress:nil transform:nil completion:nil]; + return [self initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]] options:0 cache:nil cacheKey:nil progress:nil transformType:0 transform:nil completion:nil]; } - (instancetype)initWithRequest:(NSURLRequest *)request options:(YYWebImageOptions)options - cache:(YYImageCache *)cache - cacheKey:(NSString *)cacheKey - progress:(YYWebImageProgressBlock)progress - transform:(YYWebImageTransformBlock)transform - completion:(YYWebImageCompletionBlock)completion { + cache:(nullable YYImageCache *)cache + cacheKey:(nullable NSString *)cacheKey + progress:(nullable YYWebImageProgressBlock)progress + transformType:(NSUInteger)transformType + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion { self = [super init]; if (!self) return nil; if (!request) return nil; @@ -261,6 +262,15 @@ - (instancetype)initWithRequest:(NSURLRequest *)request _shouldUseCredentialStorage = YES; _progress = progress; _transform = transform; + + if (_transform != nil) { + NSString *suffix = [NSString stringWithFormat:@"_%@", @(transformType)]; + _transformCacheKey = [_cacheKey stringByAppendingString:suffix]; + } + else { + _transformCacheKey = nil; + } + _completion = completion; _executing = NO; _finished = NO; @@ -319,7 +329,19 @@ - (void)_startOperation { if (_cache && !(_options & YYWebImageOptionUseNSURLCache) && !(_options & YYWebImageOptionRefreshImageCache)) { - UIImage *image = [_cache getImageForKey:_cacheKey withType:YYImageCacheTypeMemory]; + UIImage *image = nil; + if (_transformCacheKey) { + image = [_cache getImageForKey:_transformCacheKey withType:YYImageCacheTypeMemory]; + } + + if (image == nil) { + image = [_cache getImageForKey:_cacheKey withType:YYImageCacheTypeMemory]; + if (_transform && image) { + image = _transform(image, _request.URL); + [_cache setImage:image forKey:_transformCacheKey]; + } + } + if (image) { [_lock lock]; if (![self isCancelled]) { @@ -334,9 +356,26 @@ - (void)_startOperation { dispatch_async([self.class _imageQueue], ^{ __strong typeof(_self) self = _self; if (!self || [self isCancelled]) return; - UIImage *image = [self.cache getImageForKey:self.cacheKey withType:YYImageCacheTypeDisk]; + UIImage *image = nil; + NSString *memoryCacheKey = self.cacheKey; + + if (_transformCacheKey) { + image = [self.cache getImageForKey:self.transformCacheKey withType:YYImageCacheTypeDisk]; + if (image) { + memoryCacheKey = self.transformCacheKey; + } + } + + if (image == nil) { + image = [self.cache getImageForKey:self.cacheKey withType:YYImageCacheTypeDisk]; + if (_transform && image) { + image = _transform(image, _request.URL); + [_cache setImage:image forKey:_transformCacheKey]; + } + } + if (image) { - [self.cache setImage:image imageData:nil forKey:self.cacheKey withType:YYImageCacheTypeMemory]; + [self.cache setImage:image imageData:nil forKey:memoryCacheKey withType:YYImageCacheTypeMemory]; [self performSelector:@selector(_didReceiveImageFromDiskCache:) onThread:[self.class _networkThread] withObject:image waitUntilDone:NO]; } else { [self performSelector:@selector(_startRequest:) onThread:[self.class _networkThread] withObject:nil waitUntilDone:NO]; @@ -414,16 +453,22 @@ - (void)_didReceiveImageFromDiskCache:(UIImage *)image { [_lock unlock]; } } - -- (void)_didReceiveImageFromWeb:(UIImage *)image { +// images : "webImage", "transformImage" +- (void)_didReceiveImageFromWeb:(NSDictionary *)images { @autoreleasepool { [_lock lock]; if (![self isCancelled]) { + UIImage *image = images[@"webImage"]; + UIImage *transformImage = images[@"transformImage"]; + if (_cache) { if (image || (_options & YYWebImageOptionRefreshImageCache)) { NSData *data = _data; dispatch_async([YYWebImageOperation _imageQueue], ^{ [_cache setImage:image imageData:data forKey:_cacheKey withType:YYImageCacheTypeAll]; + if (transformImage) { + [_cache setImage:transformImage forKey:_transformCacheKey]; + } }); } } @@ -439,7 +484,7 @@ - (void)_didReceiveImageFromWeb:(UIImage *)image { } } } - if (_completion) _completion(image, _request.URL, YYWebImageFromRemote, YYWebImageStageFinished, error); + if (_completion) _completion(transformImage?:image, _request.URL, YYWebImageFromRemote, YYWebImageStageFinished, error); [self _finish]; } [_lock unlock]; @@ -694,11 +739,19 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection { if (newImage != image) { self.data = nil; } - image = newImage; + +// image = newImage; if ([self isCancelled]) return; + + // images : "webImage", "transformImage" + NSDictionary *images = @{@"webImage": image, @"transformImage":newImage}; + [self performSelector:@selector(_didReceiveImageFromWeb:) onThread:[self.class _networkThread] withObject:images waitUntilDone:NO]; + } + else { + // images : "webImage", "transformImage" + NSDictionary *images = @{@"webImage": image}; + [self performSelector:@selector(_didReceiveImageFromWeb:) onThread:[self.class _networkThread] withObject:images waitUntilDone:NO]; } - - [self performSelector:@selector(_didReceiveImageFromWeb:) onThread:[self.class _networkThread] withObject:image waitUntilDone:NO]; }); if (![self.request.URL isFileURL] && (self.options & YYWebImageOptionShowNetworkActivity)) { [YYWebImageManager decrementNetworkActivityCount]; From 685bd9e68735de612be6fb6abe11f7f835ef1c7b Mon Sep 17 00:00:00 2001 From: Homin Date: Fri, 23 Jun 2017 11:56:50 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=94=A8=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E6=9D=A5=E6=A0=87=E8=AF=86=20transform=20Image=20Cache=20Key?= =?UTF-8?q?=20=EF=BC=8C=E5=85=BC=E5=AE=B9=E6=97=A7=E7=89=88=E6=9C=ACAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Demo/YYWebImageDemo/YYTransformImageExample.m | 16 ++- Demo/YYWebImageDemo/YYWebImageExample.m | 1 + YYWebImage/Categories/CALayer+YYWebImage.h | 40 ++++++ YYWebImage/Categories/CALayer+YYWebImage.m | 40 +++++- .../Categories/MKAnnotationView+YYWebImage.h | 39 ++++++ .../Categories/MKAnnotationView+YYWebImage.m | 42 ++++++- YYWebImage/Categories/UIButton+YYWebImage.h | 90 ++++++++++++++ YYWebImage/Categories/UIButton+YYWebImage.m | 91 +++++++++++++- .../Categories/UIImageView+YYWebImage.h | 55 ++++++-- .../Categories/UIImageView+YYWebImage.m | 117 ++++++++++++------ YYWebImage/Categories/_YYWebImageSetter.h | 2 +- YYWebImage/Categories/_YYWebImageSetter.m | 4 +- YYWebImage/YYWebImageManager.h | 2 +- YYWebImage/YYWebImageManager.m | 5 +- YYWebImage/YYWebImageOperation.h | 2 +- YYWebImage/YYWebImageOperation.m | 54 ++++---- 16 files changed, 516 insertions(+), 84 deletions(-) diff --git a/Demo/YYWebImageDemo/YYTransformImageExample.m b/Demo/YYWebImageDemo/YYTransformImageExample.m index 4c08865..924d60a 100644 --- a/Demo/YYWebImageDemo/YYTransformImageExample.m +++ b/Demo/YYWebImageDemo/YYTransformImageExample.m @@ -15,6 +15,7 @@ @interface YYTransformImageExampleCell : UITableViewCell @property (nonatomic, strong) UIImageView *avatar; +@property (nonatomic, strong) UIImageView *avatar2; @end @implementation YYTransformImageExampleCell @@ -23,9 +24,12 @@ - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSStr self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { { - self.avatar = [[UIImageView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 100) / 2.0, 10, 100, 100)]; + self.avatar = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)]; [self.contentView addSubview:self.avatar]; + self.avatar2 = [[UIImageView alloc] initWithFrame:CGRectMake(120, 10, 100, 100)]; + self.avatar2.highlighted = YES; + [self.contentView addSubview:self.avatar2]; } } @@ -95,11 +99,19 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N NSInteger type = arc4random() % 5; UIColor *color = self.borderColors[type]; + + [cell.avatar yy_setImageWithURL:[NSURL URLWithString:@"https://d13yacurqjgara.cloudfront.net/users/26059/screenshots/1466318/getaway.jpg"] placeholder:[UIImage yy_imageWithColor:[UIColor lightGrayColor]] options:0 progress:nil transformId:[@(type) stringValue] transform:^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) { - [cell.avatar yy_setImageWithURL:[NSURL URLWithString:@"https://d13yacurqjgara.cloudfront.net/users/26059/screenshots/1466318/getaway.jpg"] placeholder:[UIImage yy_imageWithColor:[UIColor lightGrayColor]] options:0 progress:nil transformType:type transform:^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) { + return [image yy_imageByRoundCornerRadius:50 borderWidth:10 borderColor:color]; + } completion:nil]; + [cell.avatar2 yy_setHighlightedImageWithURL:[NSURL URLWithString:@"https://d13yacurqjgara.cloudfront.net/users/288987/screenshots/2025999/batman-beyond-the-rain.gif"] placeholder:[UIImage yy_imageWithColor:[UIColor lightGrayColor]] options:0 progress:nil transformId:[@(type) stringValue] transform:^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) { + return [image yy_imageByRoundCornerRadius:50 borderWidth:10 borderColor:color]; } completion:nil]; + + cell.avatar2.highlighted = YES; + return cell; } diff --git a/Demo/YYWebImageDemo/YYWebImageExample.m b/Demo/YYWebImageDemo/YYWebImageExample.m index bdd7522..0210e38 100644 --- a/Demo/YYWebImageDemo/YYWebImageExample.m +++ b/Demo/YYWebImageDemo/YYWebImageExample.m @@ -97,6 +97,7 @@ - (void)setImageURL:(NSURL *)url { _self.progressLayer.strokeEnd = progress; } } + transformId:nil transform:nil completion:^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) { if (stage == YYWebImageStageFinished) { diff --git a/YYWebImage/Categories/CALayer+YYWebImage.h b/YYWebImage/Categories/CALayer+YYWebImage.h index 722edd8..a7a18e5 100644 --- a/YYWebImage/Categories/CALayer+YYWebImage.h +++ b/YYWebImage/Categories/CALayer+YYWebImage.h @@ -102,6 +102,46 @@ NS_ASSUME_NONNULL_BEGIN transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; +/** + Set the view's `image` with a specified URL. + + @param imageURL The image url (remote or local file path). + @param placeholder he image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param progress The block invoked (on main thread) during image request. + @param transformId The transform Image Identifier + @param transform The block invoked (on background thread) to do additional image process. + @param completion The block invoked (on main thread) when image request completed. + */ +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion; + +/** + Set the view's `image` with a specified URL. + + @param imageURL The image url (remote or local file path). + @param placeholder he image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param manager The manager to create image request operation. + @param progress The block invoked (on main thread) during image request. + @param transformId The transform Image Identifier + @param transform The block invoked (on background thread) to do additional image process. + @param completion The block invoked (on main thread) when image request completed. + */ +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + manager:(nullable YYWebImageManager *)manager + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion; + /** Cancel the current image request. */ diff --git a/YYWebImage/Categories/CALayer+YYWebImage.m b/YYWebImage/Categories/CALayer+YYWebImage.m index 53d9b65..c4cc9b1 100644 --- a/YYWebImage/Categories/CALayer+YYWebImage.m +++ b/YYWebImage/Categories/CALayer+YYWebImage.m @@ -93,6 +93,42 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform completion:(YYWebImageCompletionBlock)completion { + [self yy_setImageWithURL:imageURL + placeholder:placeholder + options:options + manager:manager + progress:progress + transformId:@"0" + transform:transform + completion:completion]; + +} + +- (void)yy_setImageWithURL:(NSURL *)imageURL + placeholder:(UIImage *)placeholder + options:(YYWebImageOptions)options + progress:(YYWebImageProgressBlock)progress + transformId:(NSString *)transformId + transform:(YYWebImageTransformBlock)transform + completion:(YYWebImageCompletionBlock)completion { + [self yy_setImageWithURL:imageURL + placeholder:placeholder + options:options + manager:nil + progress:progress + transformId:transformId + transform:transform + completion:completion]; +} + +- (void)yy_setImageWithURL:(NSURL *)imageURL + placeholder:(UIImage *)placeholder + options:(YYWebImageOptions)options + manager:(YYWebImageManager *)manager + progress:(YYWebImageProgressBlock)progress + transformId:(NSString *)transformId + transform:(YYWebImageTransformBlock)transform + completion:(YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; manager = manager ? manager : [YYWebImageManager sharedManager]; @@ -172,8 +208,8 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL } }); }; -#warning 这里没有修改类型 - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformType:0 transform:transform completion:_completion]; + + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformId:transformId transform:transform completion:_completion]; weakSetter = setter; }); diff --git a/YYWebImage/Categories/MKAnnotationView+YYWebImage.h b/YYWebImage/Categories/MKAnnotationView+YYWebImage.h index b9bce0b..cf75bd3 100644 --- a/YYWebImage/Categories/MKAnnotationView+YYWebImage.h +++ b/YYWebImage/Categories/MKAnnotationView+YYWebImage.h @@ -99,6 +99,45 @@ NS_ASSUME_NONNULL_BEGIN transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; +/** + Set the view's `image` with a specified URL. + + @param imageURL The image url (remote or local file path). + @param placeholder he image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param progress The block invoked (on main thread) during image request. + @param transformId The transform Image Identifier + @param transform The block invoked (on background thread) to do additional image process. + @param completion The block invoked (on main thread) when image request completed. + */ +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion; +/** + Set the view's `image` with a specified URL. + + @param imageURL The image url (remote or local file path). + @param placeholder he image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param manager The manager to create image request operation. + @param progress The block invoked (on main thread) during image request. + @param transformId The transform Image Identifier + @param transform The block invoked (on background thread) to do additional image process. + @param completion The block invoked (on main thread) when image request completed. + */ +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + manager:(nullable YYWebImageManager *)manager + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion; + /** Cancel the current image request. */ diff --git a/YYWebImage/Categories/MKAnnotationView+YYWebImage.m b/YYWebImage/Categories/MKAnnotationView+YYWebImage.m index 50a1537..ff64b75 100644 --- a/YYWebImage/Categories/MKAnnotationView+YYWebImage.m +++ b/YYWebImage/Categories/MKAnnotationView+YYWebImage.m @@ -86,13 +86,51 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL completion:completion]; } + + - (void)yy_setImageWithURL:(NSURL *)imageURL placeholder:(UIImage *)placeholder options:(YYWebImageOptions)options manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform - completion:(YYWebImageCompletionBlock)completion { + completion:(YYWebImageCompletionBlock)completion{ + + [self yy_setImageWithURL:imageURL + placeholder:placeholder + options:options + manager:manager + progress:progress + transformId:@"0" + transform:transform + completion:completion]; +} + +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion { + [self yy_setImageWithURL:imageURL + placeholder:placeholder + options:options + manager:nil + progress:progress + transformId:transformId + transform:transform + completion:completion]; +} + +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + manager:(nullable YYWebImageManager *)manager + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; manager = manager ? manager : [YYWebImageManager sharedManager]; @@ -173,7 +211,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformType:0 transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformId:transformId transform:transform completion:_completion]; weakSetter = setter; }); }); diff --git a/YYWebImage/Categories/UIButton+YYWebImage.h b/YYWebImage/Categories/UIButton+YYWebImage.h index 7ad6eff..348b9f3 100644 --- a/YYWebImage/Categories/UIButton+YYWebImage.h +++ b/YYWebImage/Categories/UIButton+YYWebImage.h @@ -109,6 +109,52 @@ NS_ASSUME_NONNULL_BEGIN transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; + + +/** + Set the button's image with a specified URL for the specified state. + + @param imageURL The image url (remote or local file path). + @param state The state that uses the specified image. + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param progress The block invoked (on main thread) during image request. + @param transformId The transform Image Identifier + @param transform The block invoked (on background thread) to do additional image process. + @param completion The block invoked (on main thread) when image request completed. + */ +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + forState:(UIControlState)state + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion; + +/** + Set the button's image with a specified URL for the specified state. + + @param imageURL The image url (remote or local file path). + @param state The state that uses the specified image. + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param manager The manager to create image request operation. + @param progress The block invoked (on main thread) during image request. + @param transformId The transform Image Identifier + @param transform The block invoked (on background thread) to do additional image process. + @param completion The block invoked (on main thread) when image request completed. + */ +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + forState:(UIControlState)state + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + manager:(nullable YYWebImageManager *)manager + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion; + /** Cancel the current image request for a specified state. @param state The state that uses the specified image. @@ -202,6 +248,50 @@ NS_ASSUME_NONNULL_BEGIN transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; +/** + Set the button's backgroundImage with a specified URL for the specified state. + + @param imageURL The image url (remote or local file path). + @param state The state that uses the specified image. + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param progress The block invoked (on main thread) during image request. + @param transformId The transform Image Identifier + @param transform The block invoked (on background thread) to do additional image process. + @param completion The block invoked (on main thread) when image request completed. + */ +- (void)yy_setBackgroundImageWithURL:(nullable NSURL *)imageURL + forState:(UIControlState)state + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion; + +/** + Set the button's backgroundImage with a specified URL for the specified state. + + @param imageURL The image url (remote or local file path). + @param state The state that uses the specified image. + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param manager The manager to create image request operation. + @param progress The block invoked (on main thread) during image request. + @param transformId The transform Image Identifier + @param transform The block invoked (on background thread) to do additional image process. + @param completion The block invoked (on main thread) when image request completed. + */ +- (void)yy_setBackgroundImageWithURL:(nullable NSURL *)imageURL + forState:(UIControlState)state + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + manager:(nullable YYWebImageManager *)manager + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion; + /** Cancel the current backgroundImage request for a specified state. @param state The state that uses the specified image. diff --git a/YYWebImage/Categories/UIButton+YYWebImage.m b/YYWebImage/Categories/UIButton+YYWebImage.m index cb513f8..8980e4f 100644 --- a/YYWebImage/Categories/UIButton+YYWebImage.m +++ b/YYWebImage/Categories/UIButton+YYWebImage.m @@ -83,6 +83,7 @@ - (void)_yy_setImageWithURL:(NSURL *)imageURL options:(YYWebImageOptions)options manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress + transformId:(NSString *)transformId transform:(YYWebImageTransformBlock)transform completion:(YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; @@ -153,7 +154,7 @@ - (void)_yy_setImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformType:0 transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformId:transformId transform:transform completion:_completion]; weakSetter = setter; }); }); @@ -244,6 +245,48 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL options:options manager:manager progress:progress + transformId:@"0" + transform:transform + completion:completion]; + } +} + +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + forState:(UIControlState)state + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion { + [self yy_setImageWithURL:imageURL + forState:state + placeholder:placeholder + options:options + manager:nil + progress:progress + transformId:transformId + transform:transform + completion:completion]; +} + +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + forState:(UIControlState)state + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + manager:(nullable YYWebImageManager *)manager + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion { + for (NSNumber *num in UIControlStateMulti(state)) { + [self _yy_setImageWithURL:imageURL + forSingleState:num + placeholder:placeholder + options:options + manager:manager + progress:progress + transformId:transformId transform:transform completion:completion]; } @@ -264,6 +307,7 @@ - (void)_yy_setBackgroundImageWithURL:(NSURL *)imageURL options:(YYWebImageOptions)options manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress + transformId:(NSString *)transformId transform:(YYWebImageTransformBlock)transform completion:(YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; @@ -334,7 +378,7 @@ - (void)_yy_setBackgroundImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformType:0 transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformId:nil transform:transform completion:_completion]; weakSetter = setter; }); }); @@ -425,6 +469,49 @@ - (void)yy_setBackgroundImageWithURL:(NSURL *)imageURL options:options manager:manager progress:progress + transformId:@"0" + transform:transform + completion:completion]; + } +} + + +- (void)yy_setBackgroundImageWithURL:(nullable NSURL *)imageURL + forState:(UIControlState)state + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion { + [self yy_setBackgroundImageWithURL:imageURL + forState:state + placeholder:placeholder + options:options + manager:nil + progress:progress + transformId:transformId + transform:transform + completion:completion]; +} + +- (void)yy_setBackgroundImageWithURL:(nullable NSURL *)imageURL + forState:(UIControlState)state + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + manager:(nullable YYWebImageManager *)manager + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion { + for (NSNumber *num in UIControlStateMulti(state)) { + [self _yy_setBackgroundImageWithURL:imageURL + forSingleState:num + placeholder:placeholder + options:options + manager:manager + progress:progress + transformId:transformId transform:transform completion:completion]; } diff --git a/YYWebImage/Categories/UIImageView+YYWebImage.h b/YYWebImage/Categories/UIImageView+YYWebImage.h index 427d4bf..3aa360f 100644 --- a/YYWebImage/Categories/UIImageView+YYWebImage.h +++ b/YYWebImage/Categories/UIImageView+YYWebImage.h @@ -85,8 +85,9 @@ NS_ASSUME_NONNULL_BEGIN Set the view's `image` with a specified URL. @param imageURL The image url (remote or local file path). - @param placeholder The image to be set initially, until the image request finishes. + @param placeholder he image to be set initially, until the image request finishes. @param options The options to use when request the image. + @param manager The manager to create image request operation. @param progress The block invoked (on main thread) during image request. @param transform The block invoked (on background thread) to do additional image process. @param completion The block invoked (on main thread) when image request completed. @@ -94,33 +95,30 @@ NS_ASSUME_NONNULL_BEGIN - (void)yy_setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options + manager:(nullable YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress - transformType:(NSUInteger)transformType transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; - - /** Set the view's `image` with a specified URL. @param imageURL The image url (remote or local file path). @param placeholder he image to be set initially, until the image request finishes. @param options The options to use when request the image. - @param manager The manager to create image request operation. @param progress The block invoked (on main thread) during image request. + @param transformId The transform Image Identifier @param transform The block invoked (on background thread) to do additional image process. @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options - manager:(nullable YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; - /** Set the view's `image` with a specified URL. @@ -129,6 +127,7 @@ NS_ASSUME_NONNULL_BEGIN @param options The options to use when request the image. @param manager The manager to create image request operation. @param progress The block invoked (on main thread) during image request. + @param transformId The transform Image Identifier @param transform The block invoked (on background thread) to do additional image process. @param completion The block invoked (on main thread) when image request completed. */ @@ -137,7 +136,7 @@ NS_ASSUME_NONNULL_BEGIN options:(YYWebImageOptions)options manager:(nullable YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress - transformType:(NSUInteger)transformType + transformId:(nullable NSString *)transformId transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; @@ -224,6 +223,46 @@ NS_ASSUME_NONNULL_BEGIN transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; +/** + Set the view's `highlightedImage` with a specified URL. + + @param imageURL The image url (remote or local file path). + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param progress The block invoked (on main thread) during image request. + @param transformId The transform Image Identifier + @param transform The block invoked (on background thread) to do additional image process. + @param completion The block invoked (on main thread) when image request completed. + */ +- (void)yy_setHighlightedImageWithURL:(nullable NSURL *)imageURL + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion; + +/** + Set the view's `highlightedImage` with a specified URL. + + @param imageURL The image url (remote or local file path). + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param manager The manager to create image request operation. + @param progress The block invoked (on main thread) during image request. + @param transformId The transform Image Identifier + @param transform The block invoked (on background thread) to do additional image process. + @param completion The block invoked (on main thread) when image request completed. + */ +- (void)yy_setHighlightedImageWithURL:(nullable NSURL *)imageURL + placeholder:(nullable UIImage *)placeholder + options:(YYWebImageOptions)options + manager:(nullable YYWebImageManager *)manager + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion; + /** Cancel the current highlighed image request. */ diff --git a/YYWebImage/Categories/UIImageView+YYWebImage.m b/YYWebImage/Categories/UIImageView+YYWebImage.m index 8dc8b16..dcc7614 100644 --- a/YYWebImage/Categories/UIImageView+YYWebImage.m +++ b/YYWebImage/Categories/UIImageView+YYWebImage.m @@ -89,46 +89,40 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL completion:completion]; } -- (void)yy_setImageWithURL:(nullable NSURL *)imageURL - placeholder:(nullable UIImage *)placeholder - options:(YYWebImageOptions)options - progress:(nullable YYWebImageProgressBlock)progress - transformType:(NSUInteger)transformType - transform:(nullable YYWebImageTransformBlock)transform - completion:(nullable YYWebImageCompletionBlock)completion { +- (void)yy_setImageWithURL:(NSURL *)imageURL placeholder:(UIImage *)placeholder options:(YYWebImageOptions)options manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform completion:(YYWebImageCompletionBlock)completion { [self yy_setImageWithURL:imageURL placeholder:placeholder options:options - manager:nil + manager:manager progress:progress - transformType:transformType + transformId:@"0" transform:transform completion:completion]; } -- (void)yy_setImageWithURL:(NSURL *)imageURL - placeholder:(UIImage *)placeholder +- (void)yy_setImageWithURL:(nullable NSURL *)imageURL + placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options - manager:(YYWebImageManager *)manager - progress:(YYWebImageProgressBlock)progress - transform:(YYWebImageTransformBlock)transform - completion:(YYWebImageCompletionBlock)completion { - + progress:(nullable YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(nullable YYWebImageTransformBlock)transform + completion:(nullable YYWebImageCompletionBlock)completion { [self yy_setImageWithURL:imageURL placeholder:placeholder options:options - manager:manager + manager:nil progress:progress - transformType:0 + transformId:transformId transform:transform completion:completion]; } + - (void)yy_setImageWithURL:(NSURL *)imageURL placeholder:(UIImage *)placeholder options:(YYWebImageOptions)options manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress - transformType:(NSUInteger)transformType + transformId:(NSString *)transformId transform:(YYWebImageTransformBlock)transform completion:(YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; @@ -161,17 +155,18 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL if (manager.cache && !(options & YYWebImageOptionUseNSURLCache) && !(options & YYWebImageOptionRefreshImageCache)) { - if (transform) { - NSString *suffix = [NSString stringWithFormat:@"_%@", @(transformType)]; - NSString *cacheKey = [[manager cacheKeyForURL:imageURL] stringByAppendingString:suffix]; + if (transform && transformId) { + NSString *cacheKey = [[manager cacheKeyForURL:imageURL] stringByAppendingString:transformId]; imageFromMemory = [manager.cache getImageForKey:cacheKey withType:YYImageCacheTypeMemory]; -// if (imageFromMemory == nil) { -// imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; -// if (imageFromMemory) { -// imageFromMemory = transform(imageFromMemory, imageURL); -// [manager.cache setImage:imageFromMemory forKey:cacheKey]; -// } -// } + if (imageFromMemory == nil) { + //If you do not here for the transform to the image, try to get the original image in the transform + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + if (imageFromMemory) { + imageFromMemory = transform(imageFromMemory, imageURL); + //Cache transform Image + [manager.cache setImage:imageFromMemory forKey:cacheKey]; + } + } } else { imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; @@ -214,7 +209,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL transition.type = kCATransitionFade; [self.layer addAnimation:transition forKey:_YYWebImageFadeAnimationKey]; } - + //Here will get a good picture, can be directly displayed self.image = image; } if (completion) { @@ -227,7 +222,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformType:transformType transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformId:transformId transform:transform completion:_completion]; weakSetter = setter; }); @@ -312,6 +307,41 @@ - (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform completion:(YYWebImageCompletionBlock)completion { + [self yy_setHighlightedImageWithURL:imageURL + placeholder:placeholder + options:options + manager:manager + progress:progress + transformId:@"0" + transform:transform + completion:completion]; +} + +- (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL + placeholder:(UIImage *)placeholder + options:(YYWebImageOptions)options + progress:(YYWebImageProgressBlock)progress + transformId:(NSString *)transformId + transform:(YYWebImageTransformBlock)transform + completion:(YYWebImageCompletionBlock)completion { + [self yy_setHighlightedImageWithURL:imageURL + placeholder:placeholder + options:options + manager:nil + progress:progress + transformId:transformId + transform:transform + completion:completion]; +} + +- (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL + placeholder:(UIImage *)placeholder + options:(YYWebImageOptions)options + manager:(YYWebImageManager *)manager + progress:(YYWebImageProgressBlock)progress + transformId:(nullable NSString *)transformId + transform:(YYWebImageTransformBlock)transform + completion:(YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; manager = manager ? manager : [YYWebImageManager sharedManager]; @@ -341,8 +371,26 @@ - (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL if (manager.cache && !(options & YYWebImageOptionUseNSURLCache) && !(options & YYWebImageOptionRefreshImageCache)) { - imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + + if (transform && transformId) { + NSString *cacheKey = [[manager cacheKeyForURL:imageURL] stringByAppendingString:transformId]; + imageFromMemory = [manager.cache getImageForKey:cacheKey withType:YYImageCacheTypeMemory]; + if (imageFromMemory == nil) { + //If you do not here for the transform to the image, try to get the original image in the transform + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + if (imageFromMemory) { + imageFromMemory = transform(imageFromMemory, imageURL); + //Cache transform Image + [manager.cache setImage:imageFromMemory forKey:cacheKey]; + } + } + } + else { + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + } } + + if (imageFromMemory) { if (!(options & YYWebImageOptionAvoidSetImage)) { self.highlightedImage = imageFromMemory; @@ -391,9 +439,8 @@ - (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL } }); }; - -#warning 这里没有修改类型 - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformType:0 transform:transform completion:_completion]; + + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformId:transformId transform:transform completion:_completion]; weakSetter = setter; }); }); diff --git a/YYWebImage/Categories/_YYWebImageSetter.h b/YYWebImage/Categories/_YYWebImageSetter.h index 1dd4bc5..26ece53 100644 --- a/YYWebImage/Categories/_YYWebImageSetter.h +++ b/YYWebImage/Categories/_YYWebImageSetter.h @@ -50,7 +50,7 @@ extern const NSTimeInterval _YYWebImageProgressiveFadeTime; options:(YYWebImageOptions)options manager:(YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress - transformType:(NSUInteger)transformType + transformId:(nullable NSString *)transformId transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; diff --git a/YYWebImage/Categories/_YYWebImageSetter.m b/YYWebImage/Categories/_YYWebImageSetter.m index f3253c1..750fad0 100644 --- a/YYWebImage/Categories/_YYWebImageSetter.m +++ b/YYWebImage/Categories/_YYWebImageSetter.m @@ -48,7 +48,7 @@ - (int32_t)setOperationWithSentinel:(int32_t)sentinel options:(YYWebImageOptions)options manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress - transformType:(NSUInteger)transformType + transformId:(NSString *)transformId transform:(YYWebImageTransformBlock)transform completion:(YYWebImageCompletionBlock)completion { if (sentinel != _sentinel) { @@ -56,7 +56,7 @@ - (int32_t)setOperationWithSentinel:(int32_t)sentinel return _sentinel; } - NSOperation *operation = [manager requestImageWithURL:imageURL options:options progress:progress transformType:transformType transform:transform completion:completion]; + NSOperation *operation = [manager requestImageWithURL:imageURL options:options progress:progress transformId:transformId transform:transform completion:completion]; if (!operation && completion) { NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : @"YYWebImageOperation create failed." }; diff --git a/YYWebImage/YYWebImageManager.h b/YYWebImage/YYWebImageManager.h index 2b01ac8..3b29d2b 100644 --- a/YYWebImage/YYWebImageManager.h +++ b/YYWebImage/YYWebImageManager.h @@ -199,7 +199,7 @@ typedef void (^YYWebImageCompletionBlock)(UIImage * _Nullable image, - (nullable YYWebImageOperation *)requestImageWithURL:(NSURL *)url options:(YYWebImageOptions)options progress:(nullable YYWebImageProgressBlock)progress - transformType:(NSUInteger)transformType + transformId:(nullable NSString *)transformId transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion; diff --git a/YYWebImage/YYWebImageManager.m b/YYWebImage/YYWebImageManager.m index 5546ae6..be74c47 100644 --- a/YYWebImage/YYWebImageManager.m +++ b/YYWebImage/YYWebImageManager.m @@ -80,7 +80,7 @@ - (instancetype)initWithCache:(YYImageCache *)cache queue:(NSOperationQueue *)qu - (nullable YYWebImageOperation *)requestImageWithURL:(NSURL *)url options:(YYWebImageOptions)options progress:(nullable YYWebImageProgressBlock)progress - transformType:(NSUInteger)transformType + transformId:(nullable NSString *)transformId transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion { @@ -97,7 +97,8 @@ - (nullable YYWebImageOperation *)requestImageWithURL:(NSURL *)url cache:_cache cacheKey:[self cacheKeyForURL:url] progress:progress - transformType:transformType transform:transform ? transform : _sharedTransformBlock + transformId:transformId + transform:transform ? transform : _sharedTransformBlock completion:completion]; if (_username && _password) { diff --git a/YYWebImage/YYWebImageOperation.h b/YYWebImage/YYWebImageOperation.h index 4d96ae8..b6f495b 100644 --- a/YYWebImage/YYWebImageOperation.h +++ b/YYWebImage/YYWebImageOperation.h @@ -87,7 +87,7 @@ NS_ASSUME_NONNULL_BEGIN cache:(nullable YYImageCache *)cache cacheKey:(nullable NSString *)cacheKey progress:(nullable YYWebImageProgressBlock)progress - transformType:(NSUInteger)transformType + transformId:(nullable NSString *)transformId transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion NS_DESIGNATED_INITIALIZER; diff --git a/YYWebImage/YYWebImageOperation.m b/YYWebImage/YYWebImageOperation.m index b25b75d..24a2ddc 100644 --- a/YYWebImage/YYWebImageOperation.m +++ b/YYWebImage/YYWebImageOperation.m @@ -241,7 +241,7 @@ + (dispatch_queue_t)_imageQueue { - (instancetype)init { @throw [NSException exceptionWithName:@"YYWebImageOperation init error" reason:@"YYWebImageOperation must be initialized with a request. Use the designated initializer to init." userInfo:nil]; - return [self initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]] options:0 cache:nil cacheKey:nil progress:nil transformType:0 transform:nil completion:nil]; + return [self initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]] options:0 cache:nil cacheKey:nil progress:nil transformId:nil transform:nil completion:nil]; } - (instancetype)initWithRequest:(NSURLRequest *)request @@ -249,7 +249,7 @@ - (instancetype)initWithRequest:(NSURLRequest *)request cache:(nullable YYImageCache *)cache cacheKey:(nullable NSString *)cacheKey progress:(nullable YYWebImageProgressBlock)progress - transformType:(NSUInteger)transformType + transformId:(NSString *)transformId transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion { self = [super init]; @@ -263,9 +263,8 @@ - (instancetype)initWithRequest:(NSURLRequest *)request _progress = progress; _transform = transform; - if (_transform != nil) { - NSString *suffix = [NSString stringWithFormat:@"_%@", @(transformType)]; - _transformCacheKey = [_cacheKey stringByAppendingString:suffix]; + if (_transform && transformId) { + _transformCacheKey = [_cacheKey stringByAppendingString:transformId]; } else { _transformCacheKey = nil; @@ -325,23 +324,33 @@ - (void)_finish { - (void)_startOperation { if ([self isCancelled]) return; @autoreleasepool { + // get image from cache if (_cache && !(_options & YYWebImageOptionUseNSURLCache) && !(_options & YYWebImageOptionRefreshImageCache)) { + UIImage *image = nil; - if (_transformCacheKey) { - image = [_cache getImageForKey:_transformCacheKey withType:YYImageCacheTypeMemory]; + + //尝试从Disk里面提取 + //Try to extract oil from the Disk + if (!(_options & YYWebImageOptionIgnoreDiskCache) && + _transform && _transformCacheKey) { + //前面为了快速显示图片,只是从内存里面尝试提取图片 + //Front to display images quickly, just try to extract oil from the memory images + image = [_cache getImageForKey:_transformCacheKey withType:YYImageCacheTypeDisk]; } if (image == nil) { image = [_cache getImageForKey:_cacheKey withType:YYImageCacheTypeMemory]; - if (_transform && image) { + + if (_transform && image && _transformCacheKey) { image = _transform(image, _request.URL); [_cache setImage:image forKey:_transformCacheKey]; } } + if (image) { [_lock lock]; if (![self isCancelled]) { @@ -351,31 +360,24 @@ - (void)_startOperation { [_lock unlock]; return; } + if (!(_options & YYWebImageOptionIgnoreDiskCache)) { __weak typeof(self) _self = self; dispatch_async([self.class _imageQueue], ^{ __strong typeof(_self) self = _self; if (!self || [self isCancelled]) return; - UIImage *image = nil; - NSString *memoryCacheKey = self.cacheKey; - - if (_transformCacheKey) { - image = [self.cache getImageForKey:self.transformCacheKey withType:YYImageCacheTypeDisk]; - if (image) { - memoryCacheKey = self.transformCacheKey; - } - } - - if (image == nil) { - image = [self.cache getImageForKey:self.cacheKey withType:YYImageCacheTypeDisk]; - if (_transform && image) { - image = _transform(image, _request.URL); - [_cache setImage:image forKey:_transformCacheKey]; - } + UIImage *image = [self.cache getImageForKey:self.cacheKey withType:YYImageCacheTypeDisk]; + + if (_transform && image && _transformCacheKey) { + image = _transform(image, _request.URL); + [_cache setImage:image forKey:_transformCacheKey]; } if (image) { - [self.cache setImage:image imageData:nil forKey:memoryCacheKey withType:YYImageCacheTypeMemory]; + if (!(_transform && image && _transformCacheKey)) { + [self.cache setImage:image imageData:nil forKey:self.cacheKey withType:YYImageCacheTypeMemory]; + } + [self performSelector:@selector(_didReceiveImageFromDiskCache:) onThread:[self.class _networkThread] withObject:image waitUntilDone:NO]; } else { [self performSelector:@selector(_startRequest:) onThread:[self.class _networkThread] withObject:nil waitUntilDone:NO]; @@ -734,7 +736,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection { } if ([self isCancelled]) return; - if (self.transform && image) { + if (self.transform && image && self.transformCacheKey) { UIImage *newImage = self.transform(image, self.request.URL); if (newImage != image) { self.data = nil; From 01bef5f291b95ad9ff0a57ac280bd2949faadc14 Mon Sep 17 00:00:00 2001 From: Homin Date: Mon, 26 Jun 2017 09:45:31 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20Button=20transform=20?= =?UTF-8?q?=E4=B8=8D=E8=B5=B7=E6=95=88=E6=9E=9Cbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YYWebImage/Categories/UIButton+YYWebImage.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/YYWebImage/Categories/UIButton+YYWebImage.m b/YYWebImage/Categories/UIButton+YYWebImage.m index 8980e4f..3a6fa56 100644 --- a/YYWebImage/Categories/UIButton+YYWebImage.m +++ b/YYWebImage/Categories/UIButton+YYWebImage.m @@ -378,7 +378,7 @@ - (void)_yy_setBackgroundImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformId:nil transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transformId:transformId transform:transform completion:_completion]; weakSetter = setter; }); }); From 02a120c11922edc2be129e6fb5add07a3a19720f Mon Sep 17 00:00:00 2001 From: Homin Date: Mon, 26 Jun 2017 12:07:43 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20CALayer=20=E3=80=81MKA?= =?UTF-8?q?nnotationView=E3=80=81UIButton=20=E8=AF=BB=E5=8F=96=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=96=B9=E5=BC=8F=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YYWebImage/Categories/CALayer+YYWebImage.m | 17 ++++++++++++++++- .../Categories/MKAnnotationView+YYWebImage.m | 17 ++++++++++++++++- YYWebImage/Categories/UIButton+YYWebImage.m | 17 ++++++++++++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/YYWebImage/Categories/CALayer+YYWebImage.m b/YYWebImage/Categories/CALayer+YYWebImage.m index c4cc9b1..de86295 100644 --- a/YYWebImage/Categories/CALayer+YYWebImage.m +++ b/YYWebImage/Categories/CALayer+YYWebImage.m @@ -158,7 +158,22 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL if (manager.cache && !(options & YYWebImageOptionUseNSURLCache) && !(options & YYWebImageOptionRefreshImageCache)) { - imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + if (transform && transformId) { + NSString *cacheKey = [[manager cacheKeyForURL:imageURL] stringByAppendingString:transformId]; + imageFromMemory = [manager.cache getImageForKey:cacheKey withType:YYImageCacheTypeMemory]; + if (imageFromMemory == nil) { + //If you do not here for the transform to the image, try to get the original image in the transform + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + if (imageFromMemory) { + imageFromMemory = transform(imageFromMemory, imageURL); + //Cache transform Image + [manager.cache setImage:imageFromMemory forKey:cacheKey]; + } + } + } + else { + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + } } if (imageFromMemory) { if (!(options & YYWebImageOptionAvoidSetImage)) { diff --git a/YYWebImage/Categories/MKAnnotationView+YYWebImage.m b/YYWebImage/Categories/MKAnnotationView+YYWebImage.m index ff64b75..1028728 100644 --- a/YYWebImage/Categories/MKAnnotationView+YYWebImage.m +++ b/YYWebImage/Categories/MKAnnotationView+YYWebImage.m @@ -160,7 +160,22 @@ - (void)yy_setImageWithURL:(nullable NSURL *)imageURL if (manager.cache && !(options & YYWebImageOptionUseNSURLCache) && !(options & YYWebImageOptionRefreshImageCache)) { - imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + if (transform && transformId) { + NSString *cacheKey = [[manager cacheKeyForURL:imageURL] stringByAppendingString:transformId]; + imageFromMemory = [manager.cache getImageForKey:cacheKey withType:YYImageCacheTypeMemory]; + if (imageFromMemory == nil) { + //If you do not here for the transform to the image, try to get the original image in the transform + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + if (imageFromMemory) { + imageFromMemory = transform(imageFromMemory, imageURL); + //Cache transform Image + [manager.cache setImage:imageFromMemory forKey:cacheKey]; + } + } + } + else { + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + } } if (imageFromMemory) { if (!(options & YYWebImageOptionAvoidSetImage)) { diff --git a/YYWebImage/Categories/UIButton+YYWebImage.m b/YYWebImage/Categories/UIButton+YYWebImage.m index 3a6fa56..32abbd3 100644 --- a/YYWebImage/Categories/UIButton+YYWebImage.m +++ b/YYWebImage/Categories/UIButton+YYWebImage.m @@ -334,7 +334,22 @@ - (void)_yy_setBackgroundImageWithURL:(NSURL *)imageURL if (manager.cache && !(options & YYWebImageOptionUseNSURLCache) && !(options & YYWebImageOptionRefreshImageCache)) { - imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + if (transform && transformId) { + NSString *cacheKey = [[manager cacheKeyForURL:imageURL] stringByAppendingString:transformId]; + imageFromMemory = [manager.cache getImageForKey:cacheKey withType:YYImageCacheTypeMemory]; + if (imageFromMemory == nil) { + //If you do not here for the transform to the image, try to get the original image in the transform + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + if (imageFromMemory) { + imageFromMemory = transform(imageFromMemory, imageURL); + //Cache transform Image + [manager.cache setImage:imageFromMemory forKey:cacheKey]; + } + } + } + else { + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + } } if (imageFromMemory) { if (!(options & YYWebImageOptionAvoidSetImage)) {