SDImageCache+Resize.m 2.66 KB
Newer Older
lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
//
//  SDImageCache+Resize.m
//  iCemarose
//
//  Created by Sanchew on 2017/9/1.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "SDImageCache+Resize.h"
#import <Aspects/Aspects.h>
#import <RegexKitLite/RegexKitLite.h>
#import <SDWebImage/SDWebImageDownloader.h>
#import <AFNetworking/AFNetworking.h>

@implementation SDImageCache (Resize)

+ (void)load {
    [self aspect_hookSelector:@selector(queryDiskCacheForKey:done:) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> info){
        SDWebImageQueryCompletedBlock real = info.arguments[1];
        SDWebImageQueryCompletedBlock block = ^(UIImage *image, SDImageCacheType cacheType) {
            NSString *reg = @"imageView2/2/w/(\\d+)/h/(\\d+)";
            NSArray *sizeM = @[@"150",@"300",@"500",@"1000"];
            NSString *imageUrl = info.arguments[0];
            if (!image && [imageUrl isMatchedByRegex:reg]) {
                NSArray *sizes = [imageUrl captureComponentsMatchedByRegex:reg];
                NSUInteger si = [sizeM indexOfObject:sizes[1]];
                if (sizeM.count > si + 1) {
                    NSString *cs = sizeM[si + 1];
                    NSString *niu = [imageUrl stringByReplacingOccurrencesOfRegex:reg withString:[NSString stringWithFormat:@"imageView2/2/w/%@/h/%@",cs,cs]];
                    [info.originalInvocation.target queryDiskCacheForKey:niu done:real];
                    return;
                }
            }
            real(image,cacheType);
        };
        [info.originalInvocation setArgument:&block atIndex:3];
        [info.originalInvocation invoke];
        
    } error:nil];
}

@end


//@implementation SDWebImageDownloader (log)
////- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock
//
//+ (void)load {
//    [self aspect_hookSelector:@selector(downloadImageWithURL:options:progress:completed:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> info) {
//        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
//        NSString *url = [NSString stringWithFormat:@"http://localhost:3001?c=%@",info.arguments[0]];
//        NSURL *URL = [NSURL URLWithString:url];
//        NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//        
//        NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:nil];
//        [dataTask resume];
//    } error:nil];
//}
//
//@end