|
| 1 | +// |
| 2 | +// EsttTransformPicUrl.m |
| 3 | +// CKG |
| 4 | +// |
| 5 | +// Created by ZhJ on 16/3/3. |
| 6 | +// Copyright © 2016年 ESTT. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "EsttTransformPicUrl.h" |
| 10 | + |
| 11 | +#define BASE_HOST @"estt.com.cn" |
| 12 | +//#define BASE_HOST @"bzsns.cn" |
| 13 | +#define BASE_HOST_CHILDREN @[@"static",@"www",@"test",@"dev",@"fdfs"] |
| 14 | + |
| 15 | +@implementation EsttTransformPicUrl |
| 16 | + |
| 17 | +// 获取原生大小图片地址 |
| 18 | ++ (NSURL *)transformToOriginSize:(NSURL *)picUrl { |
| 19 | + |
| 20 | + NSString *host = [picUrl host]; |
| 21 | + |
| 22 | + BOOL isOurServer = NO; |
| 23 | + if ([host containsString:BASE_HOST]) { |
| 24 | + NSMutableArray *childrenHost = [NSMutableArray arrayWithArray:BASE_HOST_CHILDREN]; |
| 25 | + NSString *child = [host componentsSeparatedByString:@"."][0]; |
| 26 | + isOurServer = [childrenHost containsObject:child]? YES : NO; |
| 27 | + } |
| 28 | + |
| 29 | + if (isOurServer) { |
| 30 | + NSString *fileName = [[picUrl pathComponents] lastObject]; |
| 31 | + |
| 32 | + NSMutableString *newFileName = [NSMutableString stringWithString:[fileName copy]]; |
| 33 | + if ([fileName containsString:@"_"]) { |
| 34 | + newFileName = [NSMutableString stringWithString:[fileName componentsSeparatedByString:@"_"][0]]; |
| 35 | + } |
| 36 | + |
| 37 | + NSMutableString *picUrlStr = [NSMutableString stringWithString:[picUrl absoluteString]]; |
| 38 | + [picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; |
| 39 | + |
| 40 | + NSURL *newPicUrl = [NSURL URLWithString:picUrlStr]; |
| 41 | + |
| 42 | + return newPicUrl; |
| 43 | + } else { |
| 44 | + return picUrl; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// 获取固定大小图片地址 |
| 49 | ++ (NSURL *)transformUrl:(NSURL *)picUrl ToSize:(CGSize)confirmSize { |
| 50 | + |
| 51 | + NSString *host = [picUrl host]; |
| 52 | + |
| 53 | + BOOL isOurServer = NO; |
| 54 | + if ([host containsString:BASE_HOST]) { |
| 55 | + NSMutableArray *childrenHost = [NSMutableArray arrayWithArray:BASE_HOST_CHILDREN]; |
| 56 | + NSString *child = [host componentsSeparatedByString:@"."][0]; |
| 57 | + isOurServer = [childrenHost containsObject:child]? YES : NO; |
| 58 | + } |
| 59 | + |
| 60 | + if (isOurServer) { |
| 61 | + NSString *fileName = [[picUrl pathComponents] lastObject]; |
| 62 | + |
| 63 | + NSMutableString *newFileName = [NSMutableString stringWithString:[fileName copy]]; |
| 64 | + NSMutableString *picUrlStr = [NSMutableString stringWithString:[picUrl absoluteString]]; |
| 65 | + NSURL *newPicUrl; |
| 66 | + if ([fileName containsString:@"_"]) { |
| 67 | + newFileName = [NSMutableString stringWithString:[fileName componentsSeparatedByString:@"_"][0]]; |
| 68 | + [picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; |
| 69 | + newPicUrl = [NSURL URLWithString:picUrlStr]; |
| 70 | + } |
| 71 | + |
| 72 | + NSString *sizeStr = [NSString stringWithFormat:@"_%.fx%.f.png", confirmSize.width*1.5, confirmSize.height*1.5]; |
| 73 | + [newFileName appendString:sizeStr]; |
| 74 | + fileName = [[picUrl pathComponents] lastObject]; |
| 75 | + [picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; |
| 76 | + newPicUrl = [NSURL URLWithString:picUrlStr]; |
| 77 | + |
| 78 | + return newPicUrl; |
| 79 | + } else { |
| 80 | + return picUrl; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// 获取固定宽自动高图片地址 |
| 85 | ++ (NSURL *)transformUrl:(NSURL *)picUrl ToWidth:(CGFloat)width { |
| 86 | + |
| 87 | + NSString *host = [picUrl host]; |
| 88 | + |
| 89 | + BOOL isOurServer = NO; |
| 90 | + if ([host containsString:BASE_HOST]) { |
| 91 | + NSMutableArray *childrenHost = [NSMutableArray arrayWithArray:BASE_HOST_CHILDREN]; |
| 92 | + NSString *child = [host componentsSeparatedByString:@"."][0]; |
| 93 | + isOurServer = [childrenHost containsObject:child]? YES : NO; |
| 94 | + } |
| 95 | + |
| 96 | + if (isOurServer) { |
| 97 | + NSString *fileName = [[picUrl pathComponents] lastObject]; |
| 98 | + |
| 99 | + NSMutableString *newFileName = [NSMutableString stringWithString:[fileName copy]]; |
| 100 | + NSMutableString *picUrlStr = [NSMutableString stringWithString:[picUrl absoluteString]]; |
| 101 | + NSURL *newPicUrl; |
| 102 | + if ([fileName containsString:@"_"]) { |
| 103 | + newFileName = [NSMutableString stringWithString:[fileName componentsSeparatedByString:@"_"][0]]; |
| 104 | + [picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; |
| 105 | + newPicUrl = [NSURL URLWithString:picUrlStr]; |
| 106 | + } |
| 107 | + |
| 108 | + NSString *sizeStr = [NSString stringWithFormat:@"_%.f-.jpg", width*1.5]; |
| 109 | + [newFileName appendString:sizeStr]; |
| 110 | + fileName = [[picUrl pathComponents] lastObject]; |
| 111 | + [picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; |
| 112 | + newPicUrl = [NSURL URLWithString:picUrlStr]; |
| 113 | + |
| 114 | + return newPicUrl; |
| 115 | + } else { |
| 116 | + return picUrl; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// 获取固定高自动宽图片地址 |
| 121 | ++ (NSURL *)transformUrl:(NSURL *)picUrl ToHeight:(CGFloat)height { |
| 122 | + |
| 123 | + NSString *host = [picUrl host]; |
| 124 | + |
| 125 | + BOOL isOurServer = NO; |
| 126 | + if ([host containsString:BASE_HOST]) { |
| 127 | + NSMutableArray *childrenHost = [NSMutableArray arrayWithArray:BASE_HOST_CHILDREN]; |
| 128 | + NSString *child = [host componentsSeparatedByString:@"."][0]; |
| 129 | + isOurServer = [childrenHost containsObject:child]? YES : NO; |
| 130 | + } |
| 131 | + |
| 132 | + if (isOurServer) { |
| 133 | + NSString *fileName = [[picUrl pathComponents] lastObject]; |
| 134 | + |
| 135 | + NSMutableString *newFileName = [NSMutableString stringWithString:[fileName copy]]; |
| 136 | + NSMutableString *picUrlStr = [NSMutableString stringWithString:[picUrl absoluteString]]; |
| 137 | + NSURL *newPicUrl; |
| 138 | + if ([fileName containsString:@"_"]) { |
| 139 | + newFileName = [NSMutableString stringWithString:[fileName componentsSeparatedByString:@"_"][0]]; |
| 140 | + [picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; |
| 141 | + newPicUrl = [NSURL URLWithString:picUrlStr]; |
| 142 | + } |
| 143 | + |
| 144 | + NSString *sizeStr = [NSString stringWithFormat:@"_-%.f.jpg", height*1.5]; |
| 145 | + [newFileName appendString:sizeStr]; |
| 146 | + fileName = [[picUrl pathComponents] lastObject]; |
| 147 | + [picUrlStr replaceCharactersInRange:[picUrlStr rangeOfString:fileName] withString:newFileName]; |
| 148 | + newPicUrl = [NSURL URLWithString:picUrlStr]; |
| 149 | + |
| 150 | + return newPicUrl; |
| 151 | + } else { |
| 152 | + return picUrl; |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * 解析URL参数的工具方法。 |
| 158 | + */ |
| 159 | ++ (NSDictionary *)parseURLParams:(NSString *)query{ |
| 160 | + NSArray *pairs = [query componentsSeparatedByString:@"&"]; |
| 161 | + NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; |
| 162 | + for (NSString *pair in pairs) { |
| 163 | + NSArray *kv = [pair componentsSeparatedByString:@"="]; |
| 164 | + if (kv.count == 2) { |
| 165 | + NSString *val =[[kv objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
| 166 | + [params setObject:val forKey:[kv objectAtIndex:0]]; |
| 167 | + } |
| 168 | + } |
| 169 | + return params; |
| 170 | +} |
| 171 | + |
| 172 | +@end |
0 commit comments