Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// EsttTransformPicUrl.h | ||
// CKG | ||
// | ||
// Created by ZhJ on 16/3/3. | ||
// Copyright © 2016年 ESTT. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface EsttTransformPicUrl : NSObject | ||
|
||
/** | ||
* 获取原生大小图片地址 | ||
*/ | ||
+ (NSURL *)transformToOriginSize:(NSURL *)picUrl; | ||
|
||
/** | ||
* 获取固定大小图片地址 | ||
*/ | ||
+ (NSURL *)transformUrl:(NSURL *)picUrl ToSize:(CGSize)confirmSize; | ||
|
||
/** | ||
* 获取固定宽自动高图片地址 | ||
*/ | ||
+ (NSURL *)transformUrl:(NSURL *)picUrl ToWidth:(CGFloat)width; | ||
|
||
/** | ||
* 获取固定高自动宽图片地址 | ||
*/ | ||
+ (NSURL *)transformUrl:(NSURL *)picUrl ToHeight:(CGFloat)height; | ||
|
||
/** | ||
* 解析URL参数的工具方法。 | ||
*/ | ||
+ (NSDictionary *)parseURLParams:(NSString *)query; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// | ||
// EsttTransformPicUrl.m | ||
// CKG | ||
// | ||
// Created by ZhJ on 16/3/3. | ||
// Copyright © 2016年 ESTT. All rights reserved. | ||
// | ||
|
||
#import "EsttTransformPicUrl.h" | ||
|
||
#define BASE_HOST @"estt.com.cn" | ||
//#define BASE_HOST @"bzsns.cn" | ||
#define BASE_HOST_CHILDREN @[@"static",@"www",@"test",@"dev",@"fdfs"] | ||
|
||
@implementation EsttTransformPicUrl | ||
|
||
// 获取原生大小图片地址 | ||
+ (NSURL *)transformToOriginSize:(NSURL *)picUrl { | ||
|
||
NSString *host = [picUrl host]; | ||
|
||
BOOL isOurServer = NO; | ||
if ([host containsString:BASE_HOST]) { | ||
NSMutableArray *childrenHost = [NSMutableArray arrayWithArray:BASE_HOST_CHILDREN]; | ||
NSString *child = [host componentsSeparatedByString:@"."][0]; | ||
isOurServer = [childrenHost containsObject:child]? YES : NO; | ||
} | ||
|
||
if (isOurServer) { | ||
NSString *fileName = [[picUrl pathComponents] lastObject]; | ||
|
||
NSMutableString *newFileName = [NSMutableString stringWithString:[fileName copy]]; | ||
if ([fileName containsString:@"_"]) { | ||
newFileName = [NSMutableString stringWithString:[fileName componentsSeparatedByString:@"_"][0]]; | ||
} | ||
|
||
NSMutableString *picUrlStr = [NSMutableString stringWithString:[picUrl absoluteString]]; | ||
[picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; | ||
|
||
NSURL *newPicUrl = [NSURL URLWithString:picUrlStr]; | ||
|
||
return newPicUrl; | ||
} else { | ||
return picUrl; | ||
} | ||
} | ||
|
||
// 获取固定大小图片地址 | ||
+ (NSURL *)transformUrl:(NSURL *)picUrl ToSize:(CGSize)confirmSize { | ||
|
||
NSString *host = [picUrl host]; | ||
|
||
BOOL isOurServer = NO; | ||
if ([host containsString:BASE_HOST]) { | ||
NSMutableArray *childrenHost = [NSMutableArray arrayWithArray:BASE_HOST_CHILDREN]; | ||
NSString *child = [host componentsSeparatedByString:@"."][0]; | ||
isOurServer = [childrenHost containsObject:child]? YES : NO; | ||
} | ||
|
||
if (isOurServer) { | ||
NSString *fileName = [[picUrl pathComponents] lastObject]; | ||
|
||
NSMutableString *newFileName = [NSMutableString stringWithString:[fileName copy]]; | ||
NSMutableString *picUrlStr = [NSMutableString stringWithString:[picUrl absoluteString]]; | ||
NSURL *newPicUrl; | ||
if ([fileName containsString:@"_"]) { | ||
newFileName = [NSMutableString stringWithString:[fileName componentsSeparatedByString:@"_"][0]]; | ||
[picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; | ||
newPicUrl = [NSURL URLWithString:picUrlStr]; | ||
} | ||
|
||
NSString *sizeStr = [NSString stringWithFormat:@"_%.fx%.f.png", confirmSize.width*1.5, confirmSize.height*1.5]; | ||
[newFileName appendString:sizeStr]; | ||
fileName = [[picUrl pathComponents] lastObject]; | ||
[picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; | ||
newPicUrl = [NSURL URLWithString:picUrlStr]; | ||
|
||
return newPicUrl; | ||
} else { | ||
return picUrl; | ||
} | ||
} | ||
|
||
// 获取固定宽自动高图片地址 | ||
+ (NSURL *)transformUrl:(NSURL *)picUrl ToWidth:(CGFloat)width { | ||
|
||
NSString *host = [picUrl host]; | ||
|
||
BOOL isOurServer = NO; | ||
if ([host containsString:BASE_HOST]) { | ||
NSMutableArray *childrenHost = [NSMutableArray arrayWithArray:BASE_HOST_CHILDREN]; | ||
NSString *child = [host componentsSeparatedByString:@"."][0]; | ||
isOurServer = [childrenHost containsObject:child]? YES : NO; | ||
} | ||
|
||
if (isOurServer) { | ||
NSString *fileName = [[picUrl pathComponents] lastObject]; | ||
|
||
NSMutableString *newFileName = [NSMutableString stringWithString:[fileName copy]]; | ||
NSMutableString *picUrlStr = [NSMutableString stringWithString:[picUrl absoluteString]]; | ||
NSURL *newPicUrl; | ||
if ([fileName containsString:@"_"]) { | ||
newFileName = [NSMutableString stringWithString:[fileName componentsSeparatedByString:@"_"][0]]; | ||
[picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; | ||
newPicUrl = [NSURL URLWithString:picUrlStr]; | ||
} | ||
|
||
NSString *sizeStr = [NSString stringWithFormat:@"_%.f-.jpg", width*1.5]; | ||
[newFileName appendString:sizeStr]; | ||
fileName = [[picUrl pathComponents] lastObject]; | ||
[picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; | ||
newPicUrl = [NSURL URLWithString:picUrlStr]; | ||
|
||
return newPicUrl; | ||
} else { | ||
return picUrl; | ||
} | ||
} | ||
|
||
// 获取固定高自动宽图片地址 | ||
+ (NSURL *)transformUrl:(NSURL *)picUrl ToHeight:(CGFloat)height { | ||
|
||
NSString *host = [picUrl host]; | ||
|
||
BOOL isOurServer = NO; | ||
if ([host containsString:BASE_HOST]) { | ||
NSMutableArray *childrenHost = [NSMutableArray arrayWithArray:BASE_HOST_CHILDREN]; | ||
NSString *child = [host componentsSeparatedByString:@"."][0]; | ||
isOurServer = [childrenHost containsObject:child]? YES : NO; | ||
} | ||
|
||
if (isOurServer) { | ||
NSString *fileName = [[picUrl pathComponents] lastObject]; | ||
|
||
NSMutableString *newFileName = [NSMutableString stringWithString:[fileName copy]]; | ||
NSMutableString *picUrlStr = [NSMutableString stringWithString:[picUrl absoluteString]]; | ||
NSURL *newPicUrl; | ||
if ([fileName containsString:@"_"]) { | ||
newFileName = [NSMutableString stringWithString:[fileName componentsSeparatedByString:@"_"][0]]; | ||
[picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; | ||
newPicUrl = [NSURL URLWithString:picUrlStr]; | ||
} | ||
|
||
NSString *sizeStr = [NSString stringWithFormat:@"_-%.f.jpg", height*1.5]; | ||
[newFileName appendString:sizeStr]; | ||
fileName = [[picUrl pathComponents] lastObject]; | ||
[picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; | ||
newPicUrl = [NSURL URLWithString:picUrlStr]; | ||
|
||
return newPicUrl; | ||
} else { | ||
return picUrl; | ||
} | ||
} | ||
|
||
/** | ||
* 解析URL参数的工具方法。 | ||
*/ | ||
+ (NSDictionary *)parseURLParams:(NSString *)query{ | ||
NSArray *pairs = [query componentsSeparatedByString:@"&"]; | ||
NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; | ||
for (NSString *pair in pairs) { | ||
NSArray *kv = [pair componentsSeparatedByString:@"="]; | ||
if (kv.count == 2) { | ||
NSString *val =[[kv objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | ||
[params setObject:val forKey:[kv objectAtIndex:0]]; | ||
} | ||
} | ||
return params; | ||
} | ||
|
||
@end |