我正在使用HSImageSidebarView,点击图像时,如果要删除AlertView,则会弹出NSDocumentDirectory
这是删除边栏中显示的图像的方式:

-(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中的图像。

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

- (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");
}

最佳答案

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

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

10-12 00:12
查看更多