2013-08-27 84 views
-1

我想使用从目录文件夹中获取照片的CollectionView删除图片,因为我已经创建了子目录来存储图像。文件删除代码

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

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


} 

这是我的代码删除,但我必须添加我不知道的文件删除代码。

回答

1

我相信以下是你在找什么。

NSError *error; 
NSString *myFileName; 
// this is global variable 


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

    NSLog(@"ray:%d", [Trash count]); 
    NSString *myFileName = [Trash objectAtIndex:indexPath.row]; 

    NSLog(@"k%@l",myFileName); 
    [self deleteMyFiles]; 



} 

-(void) deleteMyFiles { 
    NSFileManager *fileMgr = [NSFileManager defaultManager]; 

    // Point to Document directory 
    NSString *documentsDirectory = [NSHomeDirectory() 
     stringByAppendingPathComponent:@"Documents"]; 

    NSString *filePath2 = [documentsDirectory 
          stringByAppendingPathComponent:myFileName]; 

    if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES) 
     NSLog(@"Unable to delete file: %@", [error localizedDescription]); 
} 

欲了解更多详情,请访问,

http://iosdevelopertips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html

这个环节有操作的所有实例做文件。


你得到这个是因为你没有设置错误。

NSError *myFileName;

NSError *error; 
NSString *myFileName; 
// this is global variable 
+0

这将删除文件本身之前添加NSError *error;,我只是想删除用户点击使用上面的路径之一。 – Radical

+0

@Radical:看到我更新的答案..希望这是你想要的 –

+0

我几乎在那里!如果([fileMgr removeItemAtPath:filePath2 error:&error]!= YES) NSLog(@“无法删除文件:%@”,[错误localizedDescription]);'我得到未声明的标识符错误“错误”。也没有使用变量fileMGR和filepath2 – Radical