Objective C - writeToFile 成功但 removeFile returns 相同文件的错误

Objective C - writeToFile successful but removeFile returns error for same file

我正在将用户使用 UIImagePickerController 拍摄的照片保存到用户的文档目录中。当应用程序成功上传图片到服务器后,我想删除本地保存的图片。

我像这样将图像保存到文档目录中:

/*Save image to documents directory, as opposed to core data for better memory management*/
        NSString *myUniqueName = [NSString stringWithFormat:@"%lu.png", (unsigned long)([[NSDate date] timeIntervalSince1970]*10.0)];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
        NSString *filePath = [documentsPath stringByAppendingPathComponent:myUniqueName]; //Add the file name

       BOOL successWriteLocalImage = [imageToUpload writeToFile:filePath options:NSDataWritingAtomic error:&error];

            if(successWriteLocalImage){
                //NSLog(@"no error success writing to file");
                NSLog(@"successfully write to file path %@", filePath);

            }else{
                NSLog(@"Unresolved error writing to file %@, %@", [error localizedDescription], [error userInfo]);
            }
        /*Save file path of leaf image to core data here*/
        collectedLeaf.localImageFilePath = filePath;

        [context save:&error];

我像这样删除同一个文件:

/*delete image stored in documents directory from collecting w/o internet connection*/
NSFileManager *fileManager = [NSFileManager defaultManager];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:collectedLeaf.localImageFilePath];
NSError *error;
BOOL successDeleteLocalImage = [fileManager removeItemAtPath:filePath error:&error];
if(successDeleteLocalImage){
    NSLog(@"SUCCESS DELETE IMAGE NO ERROR");
    collectedLeaf.localImageFilePath = nil;
}else{
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}

成功write/save到文档目录打印,successfully write to file path /var/mobile/Containers/Data/Application/EA08B79A-7484-4568-82CE-079B4055CDA7/Documents/15130563374.png

删除文件日志时出错

Unresolved error Error Domain=NSCocoaErrorDomain Code=4 "“15130563374.png” couldn’t be removed." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/EA08B79A-7484-4568-82CE-079B4055CDA7/Documents/var/mobile/Containers/Data/Application/EA08B79A-7484-4568-82CE-079B4055CDA7/Documents/15130563374.png, NSUserStringVariant=(
), NSUnderlyingError=0x1c4a4b4c0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}, {
    NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=2 \"No such file or directory\"";

我不确定为什么文件无法删除 - 或者 returns 找不到 - 当它与我写入的文件路径完全相同时。

首先你应该检查文件是否存在于你给的路径 使用此代码,

反对票

我认为你应该首先使用以下代码检查文件是否存在于路径中,

if ([[NSFileManager defaultManager] fileExistsAtPath:<you file path>])
{
    // then you can delete your file
}
else
{
   NSLog(@"Unresolved error %@, %@", error, [error localizedDescription])

}

如果给定路径中不存在文件,您将需要手动检查该文件,然后才能找到问题所在

问题很清楚了。您保存文​​件的文件是

/var/mobile/Containers/Data/Application/EA08B79A-7484-4568-82CE-079B4055CDA7/Documents/15130563374.png

而您要删除的位置是

/var/mobile/Containers/Data/Application/EA08B79A-7484-4568-82CE-079B4055CDA7/Documents/var/mobile/Containers/Data/Application/EA08B79A-7484-4568-82CE-079B4055CDA7/Documents/15130563374.png

很明显,您正试图在错误的路径中删除图像。

问题:

 collectedLeaf.localImageFilePath = filePath;

保存图片的绝对路径而不是相对路径。所以

NSString *filePath = [documentsPath stringByAppendingPathComponent:collectedLeaf.localImageFilePath];

将图像的绝对路径附加到文档目录路径因此导致问题

解法:

只保存核心数据中文件的唯一名称

collectedLeaf.localImageFilePath = myUniqueName;

这应该可以解决您的问题

补充信息:

永远不要在核心数据中保存文件的绝对路径。文档目录的位置将在应用程序退出和启动之间发生变化。所以保存绝对文件路径不是一个选项。保存文件名并从文件名

重建URL

我们看一下错误文件路径

NSFilePath=/var/mobile/Containers/Data/Application/EA08B79A-7484-4568-82CE-079B4055CDA7/Documents/var/mobile/Containers/Data/Application/EA08B79A-7484-4568-82CE-079B4055CDA7/Documents/15130563374.png

这个url里面有2个documentsPath。当您从 Core Data 获取要删除的文件路径时,您不需要再次附加 documentsPath。尝试用下面的代码替换删除代码

NSError *error;
BOOL successDeleteLocalImage = [fileManager removeItemAtPath:collectedLeaf.localImageFilePath error:&error];
if(successDeleteLocalImage){
  NSLog(@"SUCCESS DELETE IMAGE NO ERROR");
  collectedLeaf.localImageFilePath = nil;
}else{
  NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}