iPhone 如果文件存在问题

发布于 2024-12-06 11:24:03 字数 2403 浏览 0 评论 0原文

从我见过的所有例子来看,我确实相信我做得对。这是我的代码:

NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask, YES) objectAtIndex:0];
NSString* thisImage = [documentsPath stringByAppendingPathComponent:image_path];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:thisImage];

if (fileExists){
    hotel_image = [UIImage imageNamed:image_path];
}else{
    hotel_image = [UIImage imageNamed:@"hdrHotels.jpg"];
}

您可以假设“image_path”等于“images/hotels/thishotel.jpg”。

首先,让大家明白,“hdrHotels.jpg”位于基本目录中。 “images/hotels/thishotel.jpg”是实际的子目录(蓝色文件夹)。我还尝试过使用:

NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath = [NSString stringWithFormat:@"/%@",image_path];
NSString* thisImage = [documentsPath stringByAppendingPathComponent:imagePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:thisImage];

在“images/hotels/thishotel.jpg”前面添加一个前导“/”。在这两种情况下发生的情况是,无论图像存在与否,“if”语句都是错误的。我根本不知道我做错了什么。任何帮助将不胜感激。

编辑:

我将:更改

NSString *imagePath = [NSString stringWithFormat:@"/%@",image_path];

为:

NSString *imagePath = [image_path stringByReplacingOccurrencesOfString:@"images/hotels/" withString:@""];

但这仍然不起作用。

我也尝试过:

NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString* documentsDir = [paths objectAtIndex:0];
NSString* myFilePath = [NSString stringWithFormat:@"%@/%@", documentsDir, image_path];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myFilePath];

没有运气。

---------------------------------- 答案 ------------------------ --------------------

所以是的,我终于明白了。我无法回答我自己的问题,因为我还没有 100 次代表,但这是解决办法。

NSFileManager* fileManager = [NSFileManager defaultManager];
NSString *myFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:image_path];

BOOL fileExists = [fileManager fileExistsAtPath:myFilePath];

if (fileExists){
    hotel_image = [UIImage imageNamed:image_path];
}else{
    hotel_image = [UIImage imageNamed:@"hdrHotels.jpg"];
}

From all of the examples I've seen, I do believe I'm doing this right. Here is my code:

NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask, YES) objectAtIndex:0];
NSString* thisImage = [documentsPath stringByAppendingPathComponent:image_path];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:thisImage];

if (fileExists){
    hotel_image = [UIImage imageNamed:image_path];
}else{
    hotel_image = [UIImage imageNamed:@"hdrHotels.jpg"];
}

You can assume that "image_path" equals "images/hotels/thishotel.jpg".

Now first, so that everyone understands, "hdrHotels.jpg" is in the base directory. And "images/hotels/thishotel.jpg" are actual subdirectories (blue folers). I've also tried using:

NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath = [NSString stringWithFormat:@"/%@",image_path];
NSString* thisImage = [documentsPath stringByAppendingPathComponent:imagePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:thisImage];

Adding a leading "/" in front of "images/hotels/thishotel.jpg". What is happening in both of these cases is that the "if" statement is false whether the image exists or not. I'm not sure what I'm doing wrong at all. Any help would be greatly appreciated.

EDIT:

I changed:

NSString *imagePath = [NSString stringWithFormat:@"/%@",image_path];

to:

NSString *imagePath = [image_path stringByReplacingOccurrencesOfString:@"images/hotels/" withString:@""];

But that still didn't work.

I also tried:

NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString* documentsDir = [paths objectAtIndex:0];
NSString* myFilePath = [NSString stringWithFormat:@"%@/%@", documentsDir, image_path];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myFilePath];

with no luck.

---------------------------------- ANSWER --------------------------------

So yeah, I finally figured it out. I can't answer my own question since I don't have 100 rep yet, but here is the fix.

NSFileManager* fileManager = [NSFileManager defaultManager];
NSString *myFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:image_path];

BOOL fileExists = [fileManager fileExistsAtPath:myFilePath];

if (fileExists){
    hotel_image = [UIImage imageNamed:image_path];
}else{
    hotel_image = [UIImage imageNamed:@"hdrHotels.jpg"];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心凉 2024-12-13 11:24:03

如果您的意思是“实际子目录(蓝色文件夹)”和“基本目录”作为 Xcode 中的目录,那么所有文件都将放置在捆绑路径中,而不是放置在文档目录中。
相反,文档目录中的文件是应用程序在执行期间直接生成、复制或下载的文件。

If you mean as "actual subdirectories (blue folders)" and "base directory" as the directories in Xcode, then all of the files will be placed in the bundle path and not in the documents directory.
Instead files in the documents directory are files that have been generated or copied or downloaded directly by the application during execution.

゛时过境迁 2024-12-13 11:24:03

将前导 / 添加到 image_path,然后将其添加到 documentPath 会生成“docDir//imagePath”,文件系统将其视为“docDir/imagePath”。正如 viggio24 建议的那样,首先在 resourcesDir 中搜索图像,然后在 docDir 中搜索。

Adding a leading / to image_path and then adding that to documentPath results in "docDir//imagePath", the filesystem treats this as "docDir/imagePath". As viggio24 suggested, first search your images in the resourceDir, then in the docDir.

小姐丶请自重 2024-12-13 11:24:03

这里有四种不同的方式(临时/自定义路径、捆绑文件夹、文档文件夹和访问文档文件夹的另一种形式)

// Temporary Path Folder (read/write files)
//
NSMutableString *tempPathFile = [[NSMutableString alloc] initWithCapacity:(NSUInteger)1024];
[tempPathFile setString:[NSTemporaryDirectory() stringByAppendingPathComponent:@"export.mov"]];


// Application Folder (read-only files)
//
NSString *systemDirectoryPath = [[NSString alloc] initWithString:[[NSBundle mainBundle] resourcePath]];


// Documents Folder (read/write files)
//
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [[NSString alloc] initWithString:[filePaths objectAtIndex:0]];


// Another way to access Documents Folder (read/write files)
//
NSMutableString *documentsPath = [[NSMutableString alloc] initWithCapacity:(NSUInteger)1024];
[documentsPath setString:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];

不确定代码的其余部分发生了什么,但某些类调用的自动释放可以让您这样我通常为我自己的文件夹路径副本分配一个可变字符串

Here are four different ways (Temporary/Custom Path, Bundle Folder, Documents Folder and another form of access to Documents Folder)

// Temporary Path Folder (read/write files)
//
NSMutableString *tempPathFile = [[NSMutableString alloc] initWithCapacity:(NSUInteger)1024];
[tempPathFile setString:[NSTemporaryDirectory() stringByAppendingPathComponent:@"export.mov"]];


// Application Folder (read-only files)
//
NSString *systemDirectoryPath = [[NSString alloc] initWithString:[[NSBundle mainBundle] resourcePath]];


// Documents Folder (read/write files)
//
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [[NSString alloc] initWithString:[filePaths objectAtIndex:0]];


// Another way to access Documents Folder (read/write files)
//
NSMutableString *documentsPath = [[NSMutableString alloc] initWithCapacity:(NSUInteger)1024];
[documentsPath setString:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];

Not certain of whats going on in the rest of your code but the autorelease of some of the class calls can get you so I generally alloc a mutable string for my own copy of folder paths

下雨或天晴 2024-12-13 11:24:03

现在再次看到这个我终于可以回答了:

NSFileManager* fileManager = [NSFileManager defaultManager];
NSString *myFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:image_path];

BOOL fileExists = [fileManager fileExistsAtPath:myFilePath];

if (fileExists){
    hotel_image = [UIImage imageNamed:image_path];
}else{
    hotel_image = [UIImage imageNamed:@"hdrHotels.jpg"];
}

Now that I see this again I can finally answer it:

NSFileManager* fileManager = [NSFileManager defaultManager];
NSString *myFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:image_path];

BOOL fileExists = [fileManager fileExistsAtPath:myFilePath];

if (fileExists){
    hotel_image = [UIImage imageNamed:image_path];
}else{
    hotel_image = [UIImage imageNamed:@"hdrHotels.jpg"];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文