【问题标题】:iPhone if file exists issueiPhone如果文件存在问题
【发布时间】:2011-11-22 20:02:32
【问题描述】:

从我看到的所有示例中,我确实相信我做对了。这是我的代码:

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"];
}

【问题讨论】:

    标签: iphone objective-c file-exists


    【解决方案1】:

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

    【讨论】:

    • 那么“hotel_image = [UIImage imageNamed:image_path];”稍后用于调用图像。所以它包括“图像/酒店/”以及图像名称。但是您是说要检查图像是否存在,我需要将其剥离为图像名称?
    • 如果 img 文件包含在您的项目包中,那么您不需要 [UIImage imageNamed:] 上的路径。只是图像文件名。您可以通过检查项目 Target / Build Phase / Copy Bundle Resources 来验证图像是否在您的包中。如果文件在该列表中,则不需要路径组件
    • 是的,imageNamed 作用于名称而不作用于文件。所以它最初会检查图像缓存。如果未找到,它将在应用程序包内搜索。如果要从路径中获取图像,则必须使用 imageWithContentsOfFile: 考虑 imageNamed: 作为一种方便的方法,可以轻松查找已放置在包中并希望作为资源的图像。此外,在 iOS4 中,您也不需要指定图像扩展名(在 iOS
    • 那么你们两个的意思是它会在查找图像时自动将“images/hotels/”从字符串中剔除,这就是它找到它的原因?
    【解决方案2】:

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

    【讨论】:

    • 我知道,但我只是为了以防万一。就此而言,我对 iPhone 或任何类型的 C 语言编程完全陌生。
    【解决方案3】:

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

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

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

    【讨论】:

    • 所以对于应用程序文件夹,如果图像是捆绑包的一部分(我是否正确地假设这就是你的意思?),我是否需要剥离“图像/酒店”的 image_path 变量“?
    • 是的,您不必指定捆绑包中包含的任何内容的路径。只需要图像文件名,扩展名也是可选的。
    【解决方案4】:

    现在我又看到了,我终于可以回答了:

    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"];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 2020-07-09
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多