iOS - 将多个图像保存到文档文件夹 [英] iOS - Saving multiple images to Documents folder

查看:141
本文介绍了iOS - 将多个图像保存到文档文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,以保存图像到Documents文件夹。

I have this code this code to save an image to the Documents folder.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,        NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory  stringByAppendingPathComponent:@"savedImage.png"];
    UIImage *image = imageView.image; // imageView is my image from camera
    NSData *imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:savedImagePath atomically:NO]; 

我正在寻找一种能够保存多个图像的方法, .png name。

I am looking for a way to be able to save multiple images as this one keeps over writing the savedImage.png name.

我不介意在google或其他地方寻找它,但我需要知道它是什么,因为查找错误的关键字真的延迟世界: - )

I do not mind looking for it on google or whatever, but I need to know what it is called, since looking with the wrong keywords really delays the world:-)

干杯

推荐答案

您要附加到图像的文件名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.

这篇关于iOS - 将多个图像保存到文档文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆