Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加对不同情况Transform 后的Cache #190

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Demo/YYWebImageDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
197964141EFB9AEA002437BE /* YYTransformImageExample.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YYTransformImageExample.m; sourceTree = "<group>"; };
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 = "<group>"; };
ABC4238F1BE3244A00703518 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
1 change: 1 addition & 0 deletions Demo/YYWebImageDemo/YYImageExample.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
13 changes: 13 additions & 0 deletions Demo/YYWebImageDemo/YYTransformImageExample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// YYTransformImageExample.h
// YYWebImageDemo
//
// Created by xuemin on 2017/6/22.
// Copyright © 2017年 ibireme. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface YYTransformImageExample : UITableViewController

@end
166 changes: 166 additions & 0 deletions Demo/YYWebImageDemo/YYTransformImageExample.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
//
// 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;
@property (nonatomic, strong) UIImageView *avatar2;
@end

@implementation YYTransformImageExampleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
{
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];
}

}
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 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 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;
}

- (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
1 change: 1 addition & 0 deletions Demo/YYWebImageDemo/YYWebImageExample.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 40 additions & 0 deletions YYWebImage/Categories/CALayer+YYWebImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
57 changes: 54 additions & 3 deletions YYWebImage/Categories/CALayer+YYWebImage.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -122,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)) {
Expand Down Expand Up @@ -172,8 +223,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 transformId:transformId transform:transform completion:_completion];
weakSetter = setter;
});

Expand Down