新增iOS端图片动态转换尺寸 url 方法

This commit is contained in:
yanue
2016-03-03 14:47:58 +08:00
parent 5889754e39
commit 7f520f63da
2 changed files with 210 additions and 0 deletions

View File

@@ -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

View File

@@ -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