目标 C - writeToFile 成功但 removeFile 为同一文件返回错误 [英] Objective C - writeToFile successful but removeFile returns error for same file

查看:98
本文介绍了目标 C - writeToFile 成功但 removeFile 为同一文件返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am saving photos a user takes with UIImagePickerController to the user's documents directory. When the app successfully uploads the image to the server, I would like to delete the locally saved image.

我将图像保存到文档目录中,如下所示:

I save the image to documents directory like this:

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

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

I delete the same file like so:

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

成功写入/保存到文档目录打印,成功写入文件路径/var/mobile/Containers/Data/Application/EA08B79A-7484-4568-82CE-079B4055CDA7/Documents/15130563374.png.

The successful write/save to documents directory prints, 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\"";

当文件路径与我写入的文件路径完全相同时,我不确定为什么无法删除该文件 - 或者它返回未找到.

I'm not sure why the file can't be removed - or that it returns not found - when its the same exact file path as the one I write to.

推荐答案

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

Issue is pretty clear. The file where you saved the file is

/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

很明显,您试图在错误的路径上删除图像.

So clearly you are trying to delete image at wrong path.

问题:

 collectedLeaf.localImageFilePath = filePath;

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

Saves the images absolute path and not the relative path. So

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

会将图像的绝对路径附加到文档目录路径,从而导致问题

will append the absolute path of the image to document directory path hence results in issue

解决方案:

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

Save just the unique name of file in core data

collectedLeaf.localImageFilePath = myUniqueName;

这应该可以解决您的问题

That should solve your issue

附加信息:

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

Never ever save the absolute path of file in core data. Location of the document directory will change between app quit and launch. So saving absolute file path is not a option. Save file name and reconstruct URL from file name

这篇关于目标 C - writeToFile 成功但 removeFile 为同一文件返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆