Delete item in NSDocumentDirectory

I'm using the HSImageSidebarView, when an image is tapped, an AlertView will pop-up if you want to delete it. This is how it deletes its image shown in the sidebar:

-(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];
}

BTW, my images are from NSDocumentDirectory, but what I wanted to add is when an image is tapped in the side bar, it also deletes the image in NSDocumentDirectory.

I know this is how to delete the image in the NSDocumentDirectory, but i dont know how to use it in the above code.

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

Answers


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.


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


Need Your Help

Cannot run asp.net 5 from docker

c# asp.net linux docker asp.net-core

I have followed the following guide: Running ASP.NET 5 applications in Linux Containers with Docker and I cannot get this to work on my Windows PC or Linux server. My dockerfile looks like this:

Java Child Error in Hadoop TaskRunner

hadoop cygwin

I'm trying to run the Hadoop grep example in a pseudo-distributed configuration using Hadoop 0.22.0 on Windows 7 with Cygwin. The example works fine in standalone mode, but when run in pseudo-distr...