随机给图片命名
Posted i旅程
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随机给图片命名相关的知识,希望对你有一定的参考价值。
/*
* 随机给图片命名
* & - length 传入长度
* 返回图片名
* 随机给图片命名
* & - length 传入长度
* 返回图片名
*/
+ (NSString *)randomStringWithLength:(int)length;
/*
* 随机给图片命名
*/
+ (NSString *)randomStringWithLength:(int)length{
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity: length];
for (int i = 0; i < length; i++) {
[randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform((int)[letters length])]];
}
//获取系统时间
NSDate * senddate=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYY-MM-dd"];
NSString * locationString=[dateformatter stringFromDate:senddate];
NSDate * now = [dateformatter dateFromString:locationString];
//转成时间戳
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[now timeIntervalSince1970]];
//把时间戳和随机字符进行拼接组合
return [NSString stringWithFormat:@"%@_%@_%@.png",timeSp,randomString,@"用户uid"];
}
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity: length];
for (int i = 0; i < length; i++) {
[randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform((int)[letters length])]];
}
//获取系统时间
NSDate * senddate=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYY-MM-dd"];
NSString * locationString=[dateformatter stringFromDate:senddate];
NSDate * now = [dateformatter dateFromString:locationString];
//转成时间戳
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[now timeIntervalSince1970]];
//把时间戳和随机字符进行拼接组合
return [NSString stringWithFormat:@"%@_%@_%@.png",timeSp,randomString,@"用户uid"];
}
以上是关于随机给图片命名的主要内容,如果未能解决你的问题,请参考以下文章