使用NSDocumentDirectory从localPath中删除每个单独的图像

时间:2014-03-28 08:51:39

标签: ios objective-c

您好我将NSDocuments目录中的一些图像存储在一个文件夹中,我能够存储文件,但我无法删除每个人。当我试图删除文件完整文件夹将被删除。

以下是我要存储和删除的示例代码。

这是我正在存储文件的路径:

/Users/emantras/Library/Application Support/iPhone Simulator/7.0.3/Applications/E5D35472-6845-4BCE-98CC-3F852FF52212/Documents/uplo‌​adedpicturecard/ram_1394445189.065911.jpg

但是当我删除时,我的路径就像:

/Users/emantras/Library/Application Support/iPhone Simulator/7.0.3/Applications/E5D35472-6845-4BCE-98CC-3F852FF52212/Documents/uplo‌​adedpicturecard

你能帮助我完成我的代码吗?

-(void)saveFile
{
 UIImage *imageToUpload = [pictureCardInfo objectForKey:UIImagePickerControllerOriginalImage];
    DBWrapper *dbwrapper = [DBWrapper sharedWrapper];
    NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
    // NSTimeInterval is defined as double
    NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];

  //  ////NSLog(@"image file name: %@",timeStampObj);
    fileName1 =[_txtView.text stringByAppendingString: [NSString stringWithFormat:@"_%@",timeStampObj]];

    fileName1 = [fileName1 stringByAppendingString:@".jpg"];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
   filePath = [[documentsDirectory stringByAppendingPathComponent:@"/uploadedpicturecard"] stringByAppendingPathComponent:fileName1];

    ////NSLog(@"filePath===>%@",filePath);

    NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(imageToUpload, 1.0)];
[imageData writeToFile:filePath atomically:YES]
}

-(void)removeFile
{
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectoryPath = [paths objectAtIndex:0];

        fileName1 = [fileName1 stringByAppendingString:@".jpg"];
        filePath = [[documentsDirectoryPath stringByAppendingPathComponent:@"/uploadedpicturecard"] stringByAppendingPathComponent:fileName1];
        ////NSLog(@"FileName====>%@",filePath);

        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtPath:filePath error:NULL];
}

2 个答案:

答案 0 :(得分:1)

cOrderPath是保存所有图像的目录路径。

现在首先调用下面的代码,这将在删除之前计算所有图像数

  NSFileManager *fileManager = [NSFileManager defaultManager];
        filesForDetails = [fileManager contentsOfDirectoryAtPath:self.cOrderPath error:nil];

        iOrderCount=[filesForDetails count];
        iIndex=0;

之后调用CallForDeletion函数

[self CallForDeletion];

稍后此功能将递归删除所有图像。一旦iIndex==iOrderCount然后从函数返回

-(void)CallForDeletion
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        filesForDetails = [fileManager contentsOfDirectoryAtPath:self.cOrderPath error:nil];
         NSString *file=filesForDetails[0];
         NSString *cFilePath=[self.cOrderPath stringByAppendingPathComponent:file];
         [fileManager removeItemAtPath:cFilePath error:nil];
        iIndex=+1;

        if(iIndex==iOrderCount)
        {
          //Done with Deletion so return
        return;
        }

        [self CallForDeletion];//To call recursively
    }

答案 1 :(得分:0)

这将有效...
我品尝过它。和filePath是你的项目的完整路径 您可以使用NSLog检查文件路径是对还是错 代码是....

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
[fileManager removeItemAtPath:filePath error:&error];


if (error){
    NSLog(@"getting error now delete == %@", error);
}

希望这有帮助...