2012-08-27 55 views
1

我是从用户的照片库检索图像,并保存在文件目录中的图像。我目前正在根据用户在文本字段中输入的内容命名图片。这有效,但文本字段并不是真正的图片的好名字。我想用某种唯一的标识符来命名图片。iPhone影像保存到文档目录

任何想法或建议吗?我不想在用户保存大量照片时发生冲突。

+2

怎么样时间戳+增量? – danielbeard

+0

@danielbeard我会检查出 – Vikings

+0

是否标题居然物质(例如将用户以往任何时候都直接使用/查看文件)?如果没有,只需使用UUID。否则,请在下面查看我的答案。 –

回答

3

的一种方法是使用的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

+0

谢谢,我会看看这个 – Vikings

3

昨天我必须用几个变体解决相同的问题:保存图像临时目录,因为图片将被上传到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]; 
} 
+0

谢谢,我会看看这个 – Vikings

0

假设你的目标是什么,用户输入了要附加到某种意义照片,那么就增加对标题的末尾号,直到找到一个工程

PicnicDay.jpg

PicnicDay 1.JPG

PicnicDay 2.jpg

+0

用这种方法的问题是,我将不得不继续计数身边,所以我知道这号码,我目前 – Vikings

+0

@Vikings:不,你不该”不要这样做(因为您无法完全控制文档区域)。你只会尝试每一个,并检查它是否已经存在。如果没有,请使用该号码。 –

+0

好的,谢谢,我想我会用时间戳或UUID – Vikings