我似乎无法加载图像,但我确定它在我指定的路径中。
这是尝试加载图像的代码:
- (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 for the iPhone OS 3.0 执行和编译的。你可能从标签中得到了它,但仍然如此。
最佳答案
-[UIImage imageNamed:]
不接受图像文件的完整路径 - 它只接受文件名,然后在应用程序的主包中查找具有该文件名的图像。这就是它返回零的原因。 -[UIImage imageWithContentsOfFile:]
另一方面,需要文件的完整或部分路径。这两个构造函数之间的主要区别在于 imageNamed: 在内部缓存图像(在特定于 UIImage 的私有(private)缓存中),而 imageWithContentsOfFile: 不会缓存图像。
旁注:不,那不是内存泄漏。您正在使用一个方便的构造函数,根据 Cocoa 的内存管理规则,它应该返回一个您不拥有的自动释放对象,除非您在其他地方明确保留它。
关于iphone - UIImage 未加载但文件存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1814347/