0

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

NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:fileName]];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

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

例子:

我在这个目录下

Photo1-Photo2-Photo3 如果我删除照片 2 我想在照片 2 中重命名 Photo3

我该怎么做?

4

2 回答 2

1

你会使用这样的moveItemAtPath:toPath:error:方法NSFileManager

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;
}
于 2011-10-13T15:54:38.090 回答
1

Based on the example, you seem to be trying to store the order of the photos. While you could try enumerating the directory and check which files need to be changed and then change them, It would probably be much easier to maintain the index of the images using a plist and read the mutable array object from it and delete the indexes that need to be deleted and their respective images. The order will be retained after deletion.

于 2011-10-13T15:57:40.387 回答