首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在NSDocumentDirectory中删除项目

在NSDocumentDirectory中删除项目
EN

Stack Overflow用户
提问于 2012-06-14 13:37:24
回答 1查看 1.2K关注 0票数 1

我使用的是HSImageSidebarView,当一个图片被点击时,如果你想删除它,会弹出一个AlertView。这就是它如何删除侧边栏中显示的图像:

代码语言:javascript
复制
-(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中删除图像,但我不知道如何在上面的代码中使用它。

代码语言:javascript
复制
- (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");
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-14 14:02:35

您需要将文件名列表与您的图像一起保存。或者,在您的情况下,我在您的上一个问题中看到您的文件名是可预测的。因此,只需用Document Directory/image+index.png替换上面示例中的fullPath即可。如果不再是这种情况,那么您需要在读取文件名时跟踪它们,并将它们存储在具有相同索引的另一个数组中。

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

https://stackoverflow.com/questions/11027229

复制
相关文章

相似问题

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