博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NSURL 处理的基本函数
阅读量:4950 次
发布时间:2019-06-11

本文共 6498 字,大约阅读时间需要 21 分钟。

// 给上传图像加的水印文字常量

#define kWatermarkString @"renren-inc.com"

/**

 * 解析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 autorelease];

}

/*

 * 使用传入的baseURL地址和参数集合构造含参数的请求URL的工具方法。

 */

+ (NSURL*)generateURL:(NSString*)baseURL params:(NSDictionary*)params {

    if (params) {

        NSMutableArray* pairs = [NSMutableArray array];

        for (NSString* key in params.keyEnumerator) {

            NSString* value = [params objectForKey:key];

            NSString* escaped_value = (NSString *)CFURLCreateStringByAddingPercentEscapes(

                                                                                          NULL, /* allocator */

                                                                                          (CFStringRef)value,

                                                                                          NULL, /* charactersToLeaveUnescaped */

                                                                                          (CFStringRef)@"!*'();:@&=+$,/?%#[]",

                                                                                          kCFStringEncodingUTF8);

            

            [pairs addObject:[NSString stringWithFormat:@"%@=%@", key, escaped_value]];

            [escaped_value release];

        }

        

        NSString* query = [pairs componentsJoinedByString:@"&"];

        NSString* url = [NSString stringWithFormat:@"%@?%@", baseURL, query];

        return [NSURL URLWithString:url];

    } else {

        return [NSURL URLWithString:baseURL];

    }

}

/*

 * 根据指定的参数名,从URL中找出并返回对应的参数值。

 */

+ (NSString *)getValueStringFromUrl:(NSString *)url forParam:(NSString *)param {

    NSString * str = nil;

    NSRange start = [url rangeOfString:[param stringByAppendingString:@"="]];

    if (start.location != NSNotFound) {

        NSRange end = [[url substringFromIndex:start.location + start.length] rangeOfString:@"&"];

        NSUInteger offset = start.location+start.length;

        str = end.location == NSNotFound

        ? [url substringFromIndex:offset]

        : [url substringWithRange:NSMakeRange(offset, end.location)];

        str = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    }

    

    return str;

}

/**

 * 对输入的字符串进行MD5计算并输出验证码的工具方法。

 */

+ (NSString *)md5HexDigest:(NSString *)input{

    const char* str = [input UTF8String];

unsigned char result[CC_MD5_DIGEST_LENGTH];

CC_MD5(str, strlen(str), result);

    NSMutableString *returnHashSum = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];

    for (int i=0; i<CC_MD5_DIGEST_LENGTH; i++) {

        [returnHashSum appendFormat:@"%02x", result[i]];

    }

return returnHashSum;

}

/**

 * 计算sig码的工具方法。

 */

+ (NSString *)generateSig:(NSMutableDictionary *)paramsDict secretKey:(NSString *)secretKey{

    NSMutableString* joined = [NSMutableString string]; 

NSArray* keys = [paramsDict.allKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

for (id obj in [keys objectEnumerator]) {

id value = [paramsDict valueForKey:obj];

if ([value isKindOfClass:[NSString class]]) {

[joined appendString:obj];

[joined appendString:@"="];

[joined appendString:value];

}

}

[joined appendString:secretKey];

return [self md5HexDigest:joined];

}

/**

 * 对字符串进行URL编码转换。

 */

+ (NSString*)encodeString:(NSString*)string urlEncode:(NSStringEncoding)encoding {

    NSMutableString *escaped = [NSMutableString string];

    [escaped setString:[string stringByAddingPercentEscapesUsingEncoding:encoding]];

    

    [escaped replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@"," withString:@"%2C" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@"/" withString:@"%2F" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@":" withString:@"%3A" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@";" withString:@"%3B" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@"=" withString:@"%3D" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@"?" withString:@"%3F" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@"\t" withString:@"%09" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@"#" withString:@"%23" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@"<" withString:@"%3C" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@">" withString:@"%3E" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@"\"" withString:@"%22" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    [escaped replaceOccurrencesOfString:@"\n" withString:@"%0A" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [escaped length])];

    

    return escaped;

}

+ (NSDate *)getDateFromString:(NSString *)dateTime

{

NSDate *expirationDate =nil;

if (dateTime != nil) {

int expVal = [dateTime intValue];

if (expVal == 0) {

expirationDate = [NSDate distantFuture];

} else {

expirationDate = [NSDate dateWithTimeIntervalSinceNow:expVal];

}

return expirationDate;

}

+ (UIImage *)getImageWithWatermark:(UIImage *)inImage{

    CGRect imageViewFrame = CGRectMake(0, 0, inImage.size.width, inImage.size.height);

    UIImageView *imageView = [[UIImageView alloc] initWithImage:inImage];

    imageView.frame = imageViewFrame;

    UILabel *label = [[UILabel alloc] init];

    CGRect labelFrame = CGRectMake(10, imageViewFrame.size.height-40, imageViewFrame.size.width-20, 30);

    label.frame = labelFrame;

    label.text = kWatermarkString;

    label.textAlignment = UITextAlignmentRight;

    label.backgroundColor = [UIColor clearColor];

    [imageView addSubview:label];

    

    UIGraphicsBeginImageContext(imageViewFrame.size);

    [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *outImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    return outImage;

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/zsw-1993/archive/2012/12/17/4880564.html

你可能感兴趣的文章
sigar
查看>>
iOS7自定义statusbar和navigationbar的若干问题
查看>>
c++ 网络编程(一)TCP/UDP windows/linux 下入门级socket通信 客户端与服务端交互代码...
查看>>
程序员如何提高影响力:手把手教你塑造个人品牌
查看>>
身份证校验原理和PHP实现
查看>>
[Locked] Wiggle Sort
查看>>
deque
查看>>
计算机
查看>>
Ext JS学习第十三天 Ext基础之 Ext.Element
查看>>
python--迭代器与生成器
查看>>
SQL之case when then用法详解
查看>>
STL 排序函数
查看>>
Microsoft Dynamics CRM 2011 面向Internet部署 (IFD) ADFS虚拟机环境搭建的步骤(CRM与ADFS装在同一台服务器上) 摘自网络...
查看>>
Setting up a Passive FTP Server in Windows Azure VM(ReplyCode: 227, Entering Passive Mode )
查看>>
Atitit mtp ptp rndis midi协议的不同区别
查看>>
Ajax辅助方法
查看>>
Python模块调用
查看>>
委托的调用
查看>>
c#中从string数组转换到int数组
查看>>
Scrapy入门程序点评
查看>>