首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在NSHomeDirectory中更改文件名

在NSHomeDirectory中更改文件名
EN

Stack Overflow用户
提问于 2011-10-13 23:42:08
回答 2查看 1.4K关注 0票数 0

在我的应用程序中,我以这种方式在NSHomeDirectory中存储了一些图像:

代码语言:javascript
运行
复制
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:fileName]];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

当我删除其中一个文件时,我想重命名这些文件

示例:

我在这个目录里有

照片1-照片2-照片3如果我删除照片2,我想要重命名照片2中的Photo3

我该怎么做呢?

EN

回答 2

Stack Overflow用户

发布于 2011-10-13 23:54:38

您可以像这样使用NSFileManagermoveItemAtPath:toPath:error:方法:

代码语言:javascript
运行
复制
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];

这是未经测试的,但它应该可以工作:

代码语言:javascript
运行
复制
- (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;
}
票数 1
EN

Stack Overflow用户

发布于 2011-10-13 23:57:40

根据该示例,您似乎正在尝试存储照片的顺序。虽然您可以尝试枚举目录并检查哪些文件需要更改,然后再更改它们,但是使用plist维护图像的索引并从中读取可变数组对象并删除需要删除的索引及其相应的图像可能会容易得多。订单将在删除后保留。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7756757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档