我使用的是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");
}发布于 2012-06-14 14:02:35
您需要将文件名列表与您的图像一起保存。或者,在您的情况下,我在您的上一个问题中看到您的文件名是可预测的。因此,只需用Document Directory/image+index.png替换上面示例中的fullPath即可。如果不再是这种情况,那么您需要在读取文件名时跟踪它们,并将它们存储在具有相同索引的另一个数组中。
https://stackoverflow.com/questions/11027229
复制相似问题