如何更改NSURL文件名

Heb*_*ian 3 cocoa objective-c

也许这是一个菜鸟问题,我有一个来自savePanel的NSURL,我想用这样的标识符名称保存文件.如何更改NSURL上的文件名?

这是我的保存方法:

    for (int i=0; i<[rectCropArray count]; i++) {

   //the code goes on..

   CGImageDestinationRef dest = CGImageDestinationCreateWithURL(url, outputType, 1, nil);
   if (dest == nil) {
    NSLog(@"create CGImageDestinationRef failed");
    return NO;
   }

   CGImageDestinationAddImage(dest, imageSave, (CFDictionaryRef)dictOpts);

   //the code goes on..
  }
Run Code Online (Sandbox Code Playgroud)

我真正想要做的是,在每个循环中添加url文件名i,这样该方法可以在每个循环中保存不同的文件.例如:SavedFile1.jpg,SavedFile2.jpg ......

谢谢.

zne*_*eak 8

NSURL有一个initWithString:relativeToURL:你应该能够使用的方法.如果您获取URL的父目录,文件名和扩展名,您应该能够相对轻松地使用新URL URLWithString:relativeToURL:.

NSURL* saveDialogURL = /* fill in the blank */;
NSURL* parentDirectory = [saveDialogURL URLByDeletingLastPathComponent];
NSString* fileNameWithExtension = saveDialogURL.lastPathComponent;
NSString* fileName = [fileNameWithExtension stringByDeletingPathExtension];
NSString* extension = fileNameWithExtension.pathExtension;

for (int i = 0; i < /* fill in the blank */; i++)
{
    NSString* newFileName = [NSString stringWithFormat:@"%@-%i.%@", fileName, i, extension];
    NSURL* newURL = [NSURL URLWithString:newFileName relativeToURL:parentDirectory];
    /* do stuff with newURL */
}
Run Code Online (Sandbox Code Playgroud)