2

我想添加一个时间戳,这样当我保存应用程序时,我可以避免重复我正在使用的代码

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"]; // you maybe want to incorporate a timestamp into the name to avoid duplicates
        NSData *imageData = UIImagePNGRepresentation(captureImage.image);
        [imageData writeToFile:filePath atomically:YES];
    }
} 

如何放置时间戳或其他内容?我只是想停止重复,所以它不需要是时间戳

4

3 回答 3

3

您可以直接使用时间戳,并且没有重复的机会。检查下面的代码

time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];
NSString *timestamp=[NSString stringWithFormat:@"%ld",unixTime];
于 2013-08-17T06:21:42.607 回答
0

您没有说清楚,但我怀疑您想要添加日期作为文件名的一部分。

为此,您需要使用 " NSDateFormatter" 将 NSDate 对象(例如今天的日期?)转换为字符串。这是一个相关的问题,展示了如何创建该字符串

如果您不想将日期作为文件或文件夹名称的一部分,您也可以简单地检查该文件是否已存在于文件夹路径中。如果确实存在,请不要编写 png 文件。

于 2013-08-17T06:20:42.270 回答
0

您可以使用此NSDate选择器:

- (NSTimeInterval)timeIntervalSince1970

它返回自 1970 年 1 月 1 日 00:00:00 GMT 以来的秒数。

例子:

time_t getTime = (time_t) [[NSDate date] timeIntervalSince1970];

您可以存储它,以便您可以参考保存文件的时间。

于 2013-08-17T06:21:29.257 回答