2013-11-17 3 views
0

Я пытаюсь удалить фотографии, которые я сделал. Я сохраняю фотографии в документах. Я показываю все изображения в одном представлении коллекции и удаляю пользователя, чтобы удалить изображение. Это мой код:Не удается удалить фотографии

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

Однако только одно изображение удаляется за раз. Значение, когда первый снимок, который я использовал, только удаляется, а второе изображение, которое удаляет пользователь, не удаляется. Как мне пересмотреть код?

ответ

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) { 

    }]; 
} 
Смежные вопросы