Skip to content

Commit

Permalink
新增iOS端图片动态转换尺寸 url 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed Mar 3, 2016
1 parent 5889754 commit 7f520f6
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Objective-C/EsttTransformPicUrl.h
@@ -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
172 changes: 172 additions & 0 deletions Objective-C/EsttTransformPicUrl.m
@@ -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

0 comments on commit 7f520f6

Please sign in to comment.