【问题标题】:Counting up when saving保存时计数
【发布时间】:2013-08-17 07:03:49
【问题描述】:

我正在保存一张图片,但它只是在复制自己,因为我只设置了一个名称,但我希望它计数,这样它就不会自行替换。有没有办法做到这一点?

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

【问题讨论】:

  • 查看 hats 文件夹中的文件数量。使用 ++ 获取其计数并将其附加到要保存的文件名称。
  • 我知道它只是在替换自己,但我不知道如何使用时间戳或其他更简单的方法来阻止它

标签: ios directory save


【解决方案1】:

使用时间戳值作为文件名以避免重复

【讨论】:

  • 你的意思是这个代码仪式?time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970]; NSString *timestamp=[NSString stringWithFormat:@"%ld",unixTime];However我只是把它放在同一个地方。我认为更新图片名称不会起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
相关资源
最近更新 更多