我是從用戶的照片庫檢索圖像,並保存在文件目錄中的圖像。我目前正在根據用戶在文本字段中輸入的內容命名圖片。這有效,但文本字段並不是真正的圖片的好名字。我想用某種唯一的標識符來命名圖片。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。否則,請在下面查看我的答案。 –