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

私はこれがそれを修正するかもしれないことを願っています:

-(void)sidebar:(HSImageSidebarView *)sidebar didRemoveImageAtIndex:(NSUInteger)anIndex {
    NSLog(@ "インデックス%dの画像が削除されました"、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 * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory、NSUserDomainMask、YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    NSString * fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@ "%@。png"、fileName]];
    [fileManager removeItemAtPath:fullPathエラー:NULL];
    NSLog(@ "画像が削除されました");
}

于 2012-06-14T05:59:43.393 に答える
0

画像と一緒にファイル名のリストを保持する必要があります。または、あなたの場合、あなたの最後の質問で、あなたのファイル名は予測可能であることがわかりました。したがって、上記の例では、Document Directory / image + index.pngを置き換えてください。fullPathそうでない場合は、ファイル名を読み込んだときにファイル名を追跡し、同じインデックスを持つ別の配列に保存する必要があります。

于 2012-06-14T06:02:35.100 に答える