2012-08-27 73 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

相關問題