且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS - 将多个图像保存到文档文件夹

更新时间:2023-10-10 14:21:22

您要附加到图像的文件名documentDirectory路径在第三行。每次您需要使用一个尚未使用的其他名称。 NSFileManager有方法来查看文件是否存在,所以你可以构造一个文件名,然后测试它是否存在于该位置,如果是,增加你的重复计数,并尝试下一个。

You need to change the file name that you are appending to the image documentsDirectory path on line three. Each time you'll need to use a different name that isn't already used. NSFileManager has methods to see if a file exists so you can construct a file name and then test if it exists in that location and if so, increment your duplicate count and try the next one.

如果 num 是一个整数,你定义在某个地方,让你知道最后一个你认为你使用并且您已初始化为1某处。)

if num is an integer you define somewhere and keep around so you know the last one you thought you used (and that you've initialized to 1 somewhere).

// your code to get the directory here, as above

NSFileManager *fm = [NSFileManager ...]

do {
   savedImagePath = [documentsDirectory stringByAppendingPathComponent: 
        [NSString stringWithFormat: @"%@-%d.png", @"savedImage", num]];
   num += 1; // for next time

  if ( ![fm fileExistsAtPath: savedImagePath] )
  {
      // save your image here using savedImagePath
      exit;
  }
} while ( //some kind of test for maximum tries/time or whatever )

你必须查找语法来获取一个NSFileManager实例,确切的文件存在方法签名,但这是它的要点。

you'll have to look up the syntax to get an NSFileManager instance and the exact file exists method signature, but that's the gist of it.