2013-08-17 132 views
0

我使用的代碼然而照片時保存目錄

NSArray *directoryNames = [NSArray arrayWithObjects:@"hats",@"bottoms",@"right",@"left",nil]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 

    for (int i = 0; i < [directoryNames count] ; i++) { 
     NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:i]]; 
     if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
      [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder 

     NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; // "right" is at index 2, per comments & code 
     NSString *filePath = [folderPath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; 
     NSData *imageData = UIImagePNGRepresentation(captureImage.image); 
     [imageData writeToFile:filePath atomically:YES]; 
    } 

保存它總是會替換自己,所以我不能保存多個照片的替換。我應該如何做到這一點,以便它能夠在不重複的情況下停止這樣做?

回答

0

你一直保存到同一個地方,每驗證碼:

NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; // "right" is at index 2, per comments & code 
     NSString *filePath = [folderPath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; 
     NSData *imageData = UIImagePNGRepresentation(captureImage.image); 
     [imageData writeToFile:filePath atomically:YES]; 

通過數組循環只是保存每次都以「帽子」路徑。您已經通過dataPath對象計算了基於數組對象存儲圖像的位置。你只是沒有做任何事情。相反,請這樣做:

for (int i = 0; i < [directoryNames count] ; i++) { 
     NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:i]]; 
     if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
      [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder 
     [imageData writeToFile:dataPath atomically:YES]; 
    }