我正在从用户的照片库中检索图像,并将该图像保存在documents目录中。我目前正在根据用户在文本字段中输入的内容来命名图片。这可行,但文本字段并不是图片的好名字。我想使用某种唯一的标识符来命名图片。
有什么想法或建议吗?当用户保存大量照片时,我只是不想发生冲突。
最佳答案
一种方法是使用UUID。这是一个例子:
// return a new autoreleased UUID string
- (NSString *)generateUuidString
{
// create a new UUID which you own
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
// create a new CFStringRef (toll-free bridged to NSString)
// that you own
NSString *uuidString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
// transfer ownership of the string
// to the autorelease pool
[uuidString autorelease];
// release the UUID
CFRelease(uuid);
return uuidString;
}
或ARC版本:
// Create universally unique identifier (object)
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
// Get the string representation of CFUUID object.
NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
CFRelease(uuidObject);
更简单的iOS6 +解决方案:
NSString *UUID = [[NSUUID UUID] UUIDString];
更多信息在这里:http://blog.ablepear.com/2010/09/creating-guid-or-uuid-in-objective-c.html和这里:http://en.wikipedia.org/wiki/Universally_unique_identifier
关于iphone - iPhone将图像保存到文档目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12150959/