iphone - 删除 NSDocumentDirectory 中的项目

标签 iphone ios xcode

我正在使用 HSImageSidebarView,当点击图像时,如果您想删除它,将弹出一个 AlertView。 这是它删除侧边栏中显示的图像的方式:

-(void)sidebar:(HSImageSidebarView *)sidebar didTapImageAtIndex:(NSUInteger)anIndex {
    NSLog(@"Touched image at index: %u", anIndex);
    if (sidebar.selectedIndex == anIndex) {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Delete image?"
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                             destructiveButtonTitle:@"Delete" otherButtonTitles:nil];

        self.actionSheetBlock = ^(NSUInteger selectedIndex) {
            if (selectedIndex == sheet.destructiveButtonIndex) {
                [sidebar deleteRowAtIndex:anIndex];
                self.actionSheetBlock = nil;
            }
        };

        [sheet showFromRect:[sidebar frameOfImageAtIndex:anIndex]
                     inView:sidebar
                   animated:YES];

    }
}
- (void)sidebar:(HSImageSidebarView *)sidebar didRemoveImageAtIndex:(NSUInteger)anIndex {
    NSLog(@"Image at index %d removed", anIndex);
    [images removeObjectAtIndex:anIndex];
}

顺便说一句,我的图片来自NSDocumentDirectory,但我想添加的是当点击侧边栏中的图片时,它还会删除NSDocumentDirectory 中的图片。

我知道这是如何删除 NSDocumentDirectory 中的图像,但我不知道如何在上面的代码中使用它。

- (void)removeImage:(NSString*)fileName {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]];
    [fileManager removeItemAtPath: fullPath error:NULL];
    NSLog(@"image removed");
}

最佳答案

我希望这可以解决它:

 - (void)sidebar:(HSImageSidebarView *)sidebar didRemoveImageAtIndex:(NSUInteger)anIndex {
    NSLog(@"Image at index %d removed", anIndex);

//remove the image from Document dir
[self removeImage:[images objectAtIndex:anIndex]];

//then remove the image from the Array.
[images removeObjectAtIndex:anIndex];

}

- (void)removeImage:(NSString*)fileName {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]];
    [fileManager removeItemAtPath: fullPath error:NULL];
    NSLog(@"image removed");
}

关于iphone - 删除 NSDocumentDirectory 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11027229/

相关文章:

iphone - Core Data : Post migration, 附加迁移代码

ios - 如何保护 iOS bundle 文件,如 plist、图像、sqlite、媒体文件

ios - Xcode隐藏白色状态栏ios 10

ios - 以编程方式制作多个 CollectionView

xcode - 在界面生成器中设计时自动布局 View 调整大小的挫折

iphone - 如何制作漂亮的iOS界面?

用于开发的 Swift 包管理器

objective-c - 读写plist文件的奇怪问题

iphone - UITextField 在 iOS 5 中导致崩溃,在 iOS 4 中工作正常

iPhone - 聊天风格的应用程序需要哪些控件? (简单的问题)