开发者

How to change NSURL file name

开发者 https://www.devze.com 2023-01-16 01:41 出处:网络
Maybe this is a noob question, i have an NSURL from savePanel, and i want to save the file with such an identifier name. How to change the filename on NSURL?

Maybe this is a noob question, i have an NSURL from savePanel, and i want to save the file with such an identifier name. How to change the filename on NSURL?

here's my save method :

    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..
  }

what i really want to do is, add the url filename every loop with i, so the method can save the different file on every loop. eg: SavedFile1.jpg, SavedFile2.jpg ...

thanks.


NSURL has a initWithString:relativeToURL: method that you should be able to use for that. If you take the parent directory of the URL, the file name and the extension, you should be able to craft new URLs with relative ease using 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 */
}
0

精彩评论

暂无评论...
验证码 换一张
取 消