UIImage没有被加载但文件就在那里

Nic*_*ick 4 iphone resources objective-c uiimage

我似乎无法加载图像,但我确定它在我指定的路径上.

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

- (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

我不知道为什么它找不到该文件.一个功能看到它,另一个功能看不到,它们似乎看起来在同一个目录中......

任何帮助,非常感谢,

提前谢谢,尼克

编辑:我忘了添加代码由xcode为iPhone OS 3.0执行和编译.你可能从标签那里得到了但仍然如此.

Bry*_*nry 20

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

旁注:不,这不是内存泄漏.你正在使用一个方便构造函数,根据Cocoa的内存管理规则,它应该返回一个你没有获得所有权的自动释放对象,除非你明确地将它保留在别处.