gpt4 book ai didi

ios - Objective-C - writeToFile 成功但 removeFile 返回相同文件的错误

转载 作者:行者123 更新时间:2023-11-28 19:29:07 26 4
gpt4 key购买 nike

我正在将用户使用 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]);
}

成功写入/保存到文档目录打印,成功写入文件路径/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\"";

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

最佳答案

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

/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

关于ios - Objective-C - writeToFile 成功但 removeFile 返回相同文件的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47765940/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com