iphone - iPhone将图像保存到文档目录

标签 iphone objective-c ios

我正在从用户的照片库中检索图像,并将该图像保存在documents目录中。我目前正在根据用户在文本字段中输入的内容来命名图片。这可行,但文本字段并不是图片的好名字。我想使用某种唯一的标识符来命名图片。

有什么想法或建议吗?当用户保存大量照片时,我只是不想发生冲突。

最佳答案

一种方法是使用UUID。这是一个例子:

// return a new autoreleased UUID string
- (NSString *)generateUuidString
{
  // create a new UUID which you own
  CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);

  // create a new CFStringRef (toll-free bridged to NSString)
  // that you own
  NSString *uuidString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);

  // transfer ownership of the string
  // to the autorelease pool
  [uuidString autorelease];

  // release the UUID
  CFRelease(uuid);

  return uuidString;
}

或ARC版本:
// Create universally unique identifier (object)
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);

// Get the string representation of CFUUID object.
NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
CFRelease(uuidObject);

更简单的iOS6 +解决方案:
NSString *UUID = [[NSUUID UUID] UUIDString];

更多信息在这里:http://blog.ablepear.com/2010/09/creating-guid-or-uuid-in-objective-c.html和这里:http://en.wikipedia.org/wiki/Universally_unique_identifier

关于iphone - iPhone将图像保存到文档目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12150959/

相关文章:

objective-c - ASIHTTPRequestTester : Asynchronous not working

ios - Swift In App 订阅 如何查看活跃状态?

ios - 区别在于blktrace分析时的队列深度、磁盘偏移、iops、吞吐量等数据

iphone - 如何将图像拆分为适用于 iPhone、Android 或 Rails 的较小矩形?

iphone - UITableView 重命名每八个单元格

iphone - 将哪个参数传递给 glGet 以获取 OpenGLES iPhone 中的当前帧缓冲区

iphone - 对象C : how to insert component to url from A/B to A/between/B?

objective-c - 有什么方法可以确保 GCD 任务在没有串行队列的情况下按顺序完成?

ios - iOS应用程序之间如何通信?

ios - 如何从 super View 中删除模糊 subview