mirror of
https://github.com/yanue/nginx-lua-GraphicsMagick.git
synced 2025-11-26 23:35:33 +08:00
新增iOS端图片动态转换尺寸 url 方法
This commit is contained in:
38
Objective-C/EsttTransformPicUrl.h
Normal file
38
Objective-C/EsttTransformPicUrl.h
Normal 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
|
||||
172
Objective-C/EsttTransformPicUrl.m
Normal file
172
Objective-C/EsttTransformPicUrl.m
Normal 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
|
||||
Reference in New Issue
Block a user