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