Ios 更改NSHOME目录中文件的名称
在我的应用程序中,我以以下方式在NSHomeDirectory中存储一些图像:Ios 更改NSHOME目录中文件的名称,ios,arrays,cocoa-touch,home-directory,Ios,Arrays,Cocoa Touch,Home Directory,在我的应用程序中,我以以下方式在NSHomeDirectory中存储一些图像: NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:fileName]]; [UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES]; 我想在删除其中一个文件
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:fileName]];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
我想在删除其中一个文件时重命名这些文件
例如:
我在这个目录里有
Photo1-Photo2-Photo3如果我删除照片2,我想在照片2中重命名照片3
如何操作?您可以使用
NSFileManager的moveItemAtPath:toPath:error:
方法,如下所示:
NSString *jpgPathOne = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:@"Photo1.jpg"]];
NSString *jpgPathTwo = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:@"Photo2.jpg"]];
NSString *jpgPathThree = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:@"Photo3.jpg"]];
NSFileManager *localFileManager = [[NSFileManager alloc] init];
// ... delete Photo2
NSError *deleteError = nil;
BOOL deleted = [localFileManager removeItemAtPath:jpgPathTwo error:&deleteError];
if (!deleted || deleteError) {
NSLog(@"ERROR Deleting file: %@\n%@ - %@", jpgPathTwo, [deleteError localizedDescription], [deleteError localizedFailureReason]);
} else {
// ... If delete worked, rename Photo3 to Photo2...
NSError *renameError = nil;
BOOL renamed = [localFileManager moveItemAtPath:jpgPathThree toPath:jpgPathTwo error:&renameError];
if (!renamed || renameError) {
NSLog(@"ERROR Moving file: %@ to %@!\n%@ - %@", jpgPathThree, jpgPathTwo, [renameError localizedDescription], [renameError localizedFailureReason]);
}
}
[localFileManager release];
这是未经测试的,但它应该可以工作:
- (BOOL)deleteAndRename:(NSString *)filePath {
BOOL success = NO;
NSError *error = nil;
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:filePath]) {
success = [fileManager removeItemAtPath:filePath error:&error];
if (success) {
error = nil;
NSString *prevFilePath = filePath;
NSString *photoNumber = [[filePath stringByDeletingPathExtension] stringByReplacingOccurrencesOfString:@"Photo" withString:@""];
NSString *nextSequentialFile = [filePath stringByReplacingOccurrencesOfString:photoNumber withString:[NSString stringWithFormat:@"%d", ([photoNumber intValue] + 1)] options:NSBackwardsSearch range:NSRangeFromString(filePath)];
BOOL moveSuccess = NO;
while ([fileManager fileExistsAtPath:nextSequentialFile]) {
moveSuccess = [fileManager moveItemAtPath:nextSequentialFile toPath:prevFilePath error:&error];
if (moveSuccess) {
prevFilePath = nextSequentialFile;
photoNumber = [[prevFilePath stringByDeletingPathExtension] stringByReplacingOccurrencesOfString:@"Photo" withString:@""];
nextSequentialFile = [prevFilePath stringByReplacingOccurrencesOfString:photoNumber withString:[NSString stringWithFormat:@"%d", ([photoNumber intValue] + 1)] options:NSBackwardsSearch range:NSRangeFromString(prevFilePath)];
} else {
NSLog(@"*** Error Moving File: %@ -> %@ ***", nextSequentialFile, filePath);
if (error) {
NSLog(@"%@ - %@", [error localizedDescription], [error localizedFailureReason]);
}
success = NO;
break;
}
}
success = moveSuccess;
} else {
NSLog(@"*** Error Deleting File: %@ ***", filePath);
if (error) {
NSLog(@"%@ - %@", [error localizedDescription], [error localizedFailureReason]);
}
}
} else {
NSLog(@"*** No such file: %@ ***", filePath);
success = NO;
}
return success;
}
根据示例,您似乎试图存储照片的顺序。虽然您可以尝试枚举目录并检查哪些文件需要更改,然后再对其进行更改,但使用plist维护图像的索引并从中读取可变数组对象并删除需要删除的索引及其各自的图像可能会容易得多。删除后将保留订单。可以,但仅适用于三张照片,但适用于动态情况?存在问题;如果我有Phot1 Photo2 Photo3 Photo4 Photo5并删除Photo2,它会将Phot3重命名为Photo2,但是Photo4和Photo5?这些还必须更改名称。我在这里遇到问题:NSString nextSequentialFile=[filePath stringByReplacingOccurrencesOfString:photoNumber withString:[NSString stringWithFormat:@“%d”,([photoNumber intValue]+1)]选项:NSBackwardsSearch范围:[filePath rangeOfString:photoNumber选项:NSBackwardsSearch]//它表示由于未捕获的异常“NSRangeException”而终止应用程序,原因:“**-[NSCFString replaceOccurrencesOfString:withString:options:range::]:范围或索引超出范围”