【问题标题】:Remove each individual image from localPath by using NSDocumentDirectory使用 NSDocumentDirectory 从 localPath 中删除每个单独的图像
【发布时间】:2014-05-07 15:08:20
【问题描述】:

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

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

这是我获取 ehile 存储文件的路径:

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

【问题讨论】:

标签: ios objective-c


【解决方案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
    }

【讨论】:

  • 什么是filesForDetails、iIndex和iOrderCount?
  • filesForDetails 是 NSArray。 iIndex 和 iOrderCount 是 int
【解决方案2】:

这会起作用...
我尝过。并且 filePath 是您的项目的完整路径。
您可以使用 NSLog 检查您的文件路径是否正确。
代码是……

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


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

希望对您有所帮助...

【讨论】:

    猜你喜欢
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 2017-10-19
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多