无法删除图片

时间:2013-10-25 04:52:05

标签: ios arrays save

我将照片保存到集合视图中但我无法删除我拍摄的照片。我在集合视图中使用tap delete。这是我的代码。但是,在我点击并删除图片后,它看起来没有被删除,并且无法找到错误的地方,我怀疑它与数组有关但我不确定。< / p>

- (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 个答案:

答案 0 :(得分:1)

请务必同时删除本地内存中的图像。从collectionView中删除图像时不删除。然后停止应用程序并再次运行它看到删除的图像是否被删除?我想这是你的问题。然后将所有图像存储在单个数组中并将其存储在plist中。删除任何图像后,删除阵列中的图像后替换阵列。