Reputation: 344
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.
Upvotes: 3
Views: 7896
Reputation: 138031
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 */
}
Upvotes: 9