1

我正在使用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");
}
4

2 回答 2

1

I hope this may fix it:

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

于 2012-06-14T05:59:43.393 回答
0

You need to keep a list of file names along with your images. Or, in your case, I saw in your last question that your filenames are predictable. So just substitute Document Directory/image+index.png for fullPath in the above example. If that is not the case anymore, then you need to keep track of the filenames when you read them in, and store them in another array with the same indices.

于 2012-06-14T06:02:35.100 回答