代码之家  ›  专栏  ›  技术社区  ›  Nick

未加载UIImage,但存在文件

  •  4
  • Nick  · 技术社区  · 15 年前

    以下是尝试加载图像的代码:

    - (UIImage*) CPPlistImageToUIImage: (NSString *) CPPlistImage {
        /*TODO: Is this a memory leak? Find out*/
        UIImage * ret = [UIImage imageNamed: CPPlistImage];
        if(ret == nil){
            RDLogString(@"Warning: Failed to load image: \"%@\"", CPPlistImage);
        }
    
        return ret;
    }

    - (void) lsOwnDirectory {
        NSError * error = [[NSError alloc] init];
        NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: pathToOwnDirectory error: &error];
    
        for(NSString * file in files){
            RDLogString(@"%@", file);
        }
    }

    下面是两个函数在同一次运行中的输出示例:

    // CPPlist...Image telling he can't find/load the image
    2009-11-29 02:12:51.510 CPPlayer[6452:207] CPConversation.m:113 Warning: Failed to load image: "/Users/nick/Library/Application Support/iPhone Simulator/User/Applications/D3403591-3A56-4F05-B4C1-C21DAAF918A2/CPPlayer.app/CP.Resources/CP.Conversations/Supermarkt/OIGNONS.png"
    ...
    // lsOwnDirectory is called AFTER CPPList...Image
    2009-11-29 02:12:51.557 CPPlayer[6452:207] RootViewController.m:81 Contents of /Users/nick/Library/Application Support/iPhone Simulator/User/Applications/D3403591-3A56-4F05-B4C1-C21DAAF918A2/CPPlayer.app/CP.Resources/CP.Conversations/Supermarkt
    2009-11-29 02:12:51.566 CPPlayer[6452:207] CPConversation.m:133 audio
    2009-11-29 02:12:51.567 CPPlayer[6452:207] CPConversation.m:133 CPConversation.plist
    2009-11-29 02:12:51.568 CPPlayer[6452:207] CPConversation.m:133 CPData.sqlite
    2009-11-29 02:12:51.569 CPPlayer[6452:207] CPConversation.m:133 images
    2009-11-29 02:12:51.570 CPPlayer[6452:207] CPConversation.m:133 OIGNONS.png
    

    非常感谢您的帮助,

    刻痕

    1 回复  |  直到 15 年前
        1
  •  20
  •   Bryan Henry    15 年前

    -[UIImage imageNamed:] 不接受图像文件的完整路径-它只接受文件名,然后在应用程序的主捆绑包中查找具有该文件名的图像。这就是它返回零的原因。 -[UIImage imageWithContentsOfFile:] 另一方面,需要文件的完整或部分路径。这两个构造函数之间的主要区别在于ImageName:在内部缓存图像(在特定于UIImage的专用缓存中),而imageWithContentsOfFile:不会缓存图像。