一、MD5的封装
1 #define CC_MD5_DIGEST_LENGTH 16 2 3 - (NSString *)toMD5 4 { 5 const char* input = [self UTF8String]; 6 unsigned char result[CC_MD5_DIGEST_LENGTH]; 7 CC_MD5(input, (CC_LONG)strlen(input), result); 8 9 NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; 10 for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { 11 [digest appendFormat:@"%02x", result[i]]; 12 } 13 14 return digest; 15 }
二、生成独一无二的图片名称
1)根据URLString来生成
1 - (NSString*) genImageName:(NSString *)urlString 2 { 3 NSString *imageName = [urlString toMD5]; 4 return [imageName stringByAppendingPathExtension:@"png"]; 5 }
2) 根据时间戳来生成(绝对独一无二)
1 +(NSString *)createCUID 2 { 3 NSString *prefix = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]; 4 NSString * result; 5 CFUUIDRef uuid; 6 CFStringRef uuidStr; 7 uuid = CFUUIDCreate(NULL); 8 uuidStr = CFUUIDCreateString(NULL, uuid); 9 result =[NSString stringWithFormat:@"%@-%@",prefix,uuidStr]; 10 CFRelease(uuidStr); 11 CFRelease(uuid); 12 return [result toMD5]; 13 } 14 15 //生成图片名,统一用png 16 static inline NSString* genImageName() 17 { 18 NSString *imageName = [NSString createCUID]; 19 return [imageName stringByAppendingPathExtension:@"png"]; 20 }
时间: 2024-11-02 23:10:33