gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:06:31 24 4
gpt4 key购买 nike

您好,我将一些图像存储在 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];
}

最佳答案

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
}

关于ios - 使用 NSDocumentDirectory 从 localPath 中删除每个单独的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22708018/

24 4 0
文章推荐: php - 添加限制加速 mysql 查询,为什么?
文章推荐: java - 在 Java 中使用 Scala - 如何将 Java 对象转换为 Option