我是从用户的照片库检索图像,并保存在文件目录中的图像。我目前正在根据用户在文本字段中输入的内容命名图片。这有效,但文本字段并不是真正的图片的好名字。我想用某种唯一的标识符来命名图片。iPhone影像保存到文档目录
任何想法或建议吗?我不想在用户保存大量照片时发生冲突。
我是从用户的照片库检索图像,并保存在文件目录中的图像。我目前正在根据用户在文本字段中输入的内容命名图片。这有效,但文本字段并不是真正的图片的好名字。我想用某种唯一的标识符来命名图片。iPhone影像保存到文档目录
任何想法或建议吗?我不想在用户保存大量照片时发生冲突。
的一种方法是使用的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;
}
或圆弧版本:
// 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
谢谢,我会看看这个 – Vikings
昨天我必须用几个变体解决相同的问题:保存图像临时目录,因为图片将被上传到Dropbox。
我所做的是得到的秒数从unix新纪元重命名图像。
这里是整个方法。您将需要对其进行修改,以满足您的需求,但你应该得到的要领来解决你的问题出它:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *imageToUpload = [info objectForKey:UIImagePickerControllerOriginalImage];
NSDate *dateForPictureName = [NSDate date];
NSTimeInterval timeInterval = [dateForPictureName timeIntervalSince1970];
NSMutableString *fileName = [NSMutableString stringWithFormat:@"%f", timeInterval];
NSRange thePeriod = [fileName rangeOfString:@"."]; //Epoch returns with a period for some reason.
[fileName deleteCharactersInRange:thePeriod];
[fileName appendString:@".jpeg"];
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(imageToUpload, 1.0)];
[imageData writeToFile:filePath atomically:YES];
[[self restClient] uploadFile:fileName toPath:currentPath withParentRev:nil fromPath:filePath];
[picker dismissModalViewControllerAnimated:YES];
}
谢谢,我会看看这个 – Vikings
怎么样时间戳+增量? – danielbeard
@danielbeard我会检查出 – Vikings
是否标题居然物质(例如将用户以往任何时候都直接使用/查看文件)?如果没有,只需使用UUID。否则,请在下面查看我的答案。 –