2012-07-11 81 views
0

嗨我真的很難節省我的NSDocumentDirectory。我使用AGImagePicker順便說一句。是的,我可以保存在NSDocumentDirectory。但是,如何將它們保存爲唯一的(就其自身而言,然後將它們的名稱轉換爲oneSlotImages),或者將它們保存爲唯一的ID,然後將它們加載回去。對不起,我有點新的UIImagePickerControllerMediaURL事情,我認爲這將是我的解決方案,我的另一個問題是保存時不重疊他們。如何使用其唯一的ID或UIImagePickerControllerMediaURL進行保存。唯一地保存在NSDocumentDirectory

 for (int i = 0; i < info.count; i++) { 
      NSLog(@"%@", [info objectAtIndex:i]); 
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES); 
      NSString *documentsDir = [paths objectAtIndex:0]; 
      NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%d.png", i]]; 


      ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation]; 
      UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]]; 

      //----resize the images 
      image = [self imageByScalingAndCroppingForSize:image toSize:CGSizeMake(256,256*image.size.height/image.size.width)]; 

      NSData *imageData = UIImagePNGRepresentation(image); 
      [imageData writeToFile:savedImagePath atomically:YES]; 

感謝您的幫助。非常感激。

回答

0

每張圖片的URL是唯一的嗎?所以我們可以利用這個。將URL轉換爲MD5字符串(它形成每個圖像的唯一標識符)。並保存該名稱(如「MD5string.png」)。

爲什麼我們不能用這個? 希望這可以幫助你。

對於轉換爲MD5,請創建一個名爲的NSString + MD5.h文件,並把代碼

#import <Foundation/Foundation.h> 
#import <CommonCrypto/CommonDigest.h> 


@interface NSString(MD5) 

- (NSString *)MD5; 

@end 

在裏面。 然後在的NSString + MD5.m,

#import "NSString+MD5.h" 

@implementation NSString(MD5) 

- (NSString*)MD5 
{ 
    const char *ptr = [self UTF8String]; 

    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH]; 

    CC_MD5(ptr, strlen(ptr), md5Buffer); 
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; 
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) 
     [output appendFormat:@"%02x",md5Buffer[i]]; 

    return output; 
} 
@end 

進口在任何你想使用的MD5函數與正常NSString對象NSString+MD5.h類。

在你的代碼,如果你有UIImagePickerControllerMediaURL字符串,因爲它是唯一的每一個文件,你可以轉換爲字符串的MD5像

NSString *imgURL = [NSString stringWithString: UIImagePickerControllerMediaURL]; 
NSString *MD5String = [imgURL MD5]; 
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", MD5String]]; 

使用該路徑來保存圖像文件。

上的裝入,

轉換的UIImagePickerControllerMediaURL成MD5和文檔目錄檢查該文件

// In this the UIImagePickerControllerMediaURL is the URL of media file to load 
NSString *imgURL = [NSString stringWithString: UIImagePickerControllerMediaURL]; 
NSString *MD5String = [imgURL MD5]; 
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", MD5String]]; 

然後做的NSFileManager的fileExistsAtPath方法的檢查,如果存在,然後加載從文件路徑。而已。

注意:對於這一點,你要保持你的文檔目錄當地某處你的應用程序中保存的圖像(DB NSUserDefaults的或)爲化妝用的那麼的UIImagePickerControllerMediaURL在裝貨的時間。

+0

如何在我的代碼?以及如何加載它們 – Bazinga 2012-07-11 06:45:10

+0

請參閱編輯。 – 2012-07-11 06:53:22

+0

然後在加載時? – Bazinga 2012-07-11 06:55:04

1

你總是可以保持使用的名稱的列表,這樣做

int i = 1; 

while([listOfUsedNames containsObject:nextAvailableTile]) { 

     nextAvailableTitle = [kDefaultImageName stringByAppendingFormat:@" %d", i]; 
     i++; 
} 

//找到一個未使用的名稱

+0

我認爲這與檢查是否一樣文件已經存在與該文件名。 – Bazinga 2012-07-11 06:46:33

0

試試這個:

NSMutableString *imageName = [[[NSMutableString alloc] initWithCapacity:0] autorelease]; 

CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault); 
if (theUUID) { 
    [imageName appendString:NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID))]; 
      CFRelease(theUUID); 
} 
[imageName appendString:@".png"];