无法删除图片

时间:2013-11-17 03:35:08

标签: ios uicollectionview

我正在尝试删除我拍摄的照片。我正在保存文件中的图片。我在一个集合视图中显示所有图片,并让用户点击删除以删除图片。这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    allImagesArray = [[NSMutableArray alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *locations = [[NSArray alloc]initWithObjects:@"Bottoms", @"Dress", @"Coats", @"Others", @"hats", @"Tops",nil ];
    NSString *fPath = documentsDirectory;
    NSMutableArray *trashCan = [NSMutableArray array];
    NSArray *directoryContent;
    for(NSString *component in locations){
        NSString *TrashBin = [fPath stringByAppendingPathComponent:component];
        NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: TrashBin];
        collectionTrash.delegate =self;
        collectionTrash.dataSource=self;
        for(NSString *str in directoryContent){
            NSLog(@"str:%@", str);
            NSString *finalFilePath = [TrashBin stringByAppendingPathComponent:str];
            NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
            [trashCan addObject:finalFilePath];
            if(data)
            {
                UIImage *image = [UIImage imageWithData:data];
                [allImagesArray addObject:image];
                NSLog(@"array:%@",[allImagesArray description]);
            }}}
    Trash = trashCan;
    for(NSString *folder in locations) {

        for(NSString *file in directoryContent) {

            // load the image

        }
    }}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    NSLog(@"j");
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [allImagesArray count];


}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseID = @"ReuseID";
    TrashCell *mycell = (TrashCell *) [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
    UIImageView *imageInCell = (UIImageView*)[mycell viewWithTag:1];
    imageInCell.image = [allImagesArray objectAtIndex:indexPath.row];
    NSLog(@"a");
    return mycell;
}
//この下のコードではタップ消去をするためのコードです。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"s:%d", [Trash count]);
    NSString *trashBin = [Trash objectAtIndex:indexPath.row];
    NSLog(@"k%@l",trashBin);
    [allImagesArray removeObjectAtIndex:indexPath.row];
    [self deleteMyFiles:trashBin];
    [collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]];
}
NSString *myFileName;
-(void) deleteMyFiles:(NSString*)filePath {
    NSError *error;

    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
    }
}

但是,一次只删除一张图片。当我点击的第一张图片被删除而用户点击的第二张图片没有被删除时的含义。我该如何修改我的代码?

1 个答案:

答案 0 :(得分:0)

我认为您忘记更新垃圾桶阵列,只需尝试使用此代码:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    [collectionView performBatchUpdates:^{

        NSLog(@"s:%d", [Trash count]);
        NSString *trashBin = [Trash objectAtIndex:indexPath.row];
        NSLog(@"k%@l",trashBin);

        //Don't forget to update Trash here:
        [Trash removeObjectAtIndex:indexPath.row];

        [allImagesArray removeObjectAtIndex:indexPath.row];
        [self deleteMyFiles:trashBin];
        [collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]];

    } completion:^(BOOL finished) {

    }];
}