2013-10-25 125 views
-1

我正在將圖片保存到收藏視圖中,但無法刪除已拍攝的圖片。我正在集合視圖中使用點擊刪除。這是我的代碼。但是,當我點擊並刪除圖片後,它看起來像圖片沒有被刪除,並且找不到錯在哪裏,我懷疑它與數組有關,但我不確定。無法刪除圖片

- (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.collectionTrash reloadData]; 
    [self deleteMyFiles:trashBin]; 
} 
NSString *myFileName; 
-(void) deleteMyFiles:(NSString*)filePath { 
    NSError *error; 

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

回答

1

請務必刪除本地內存中的圖像。當你從collectionView中刪除圖像時不刪除。然後停止應用程序並再次運行它看到刪除的圖像被刪除或不?我認爲這是你的問題。然後將所有圖像存儲在單個數組中並將其存儲在plist中。刪除任何圖像後,請在刪除陣列中的圖像後替換陣列。

0

使用

[collectionView reloadData] 

調用deleteMyFiles後。