文件存在但不会加载?

时间:2010-12-08 08:26:30

标签: iphone ios uiimage nsfilemanager

我已经在我的资源包中添加了一个资源(蓝色文件夹),所有这些似乎都很好甚至到了我使用NSFileManger确定文件是否存在以及是否打印出检查中使用的完整路径的位置是这样的:

NSString *p = [NSString stringWithFormat:@"%@/words/001", 
   [[NSBundle mainBundle] resourcePath]];

NSFileManager *fm = [NSFileManager defaultManager];

NSString *a = [p stringByAppendingPathComponent:@"001-1.png"];

UIImage *i;

if ([fm fileExistsAtPath:a]) {

  NSLog(@"file found ... %@", a);

  i = [UIImage imageWithContentsOfFile:a];
}
else {

  NSLog(@"doh!");
} 

[iv setImage:i];

我从文件存在检查

获得此输出

/ Users / phh / Library / Application Support / iPhone Simulator / 4.2 / Applications / 3DAB4772-7251-4E39-8EA8-4AFE32DFB61A / MyApp.app / words / 001 / 001-1.png

但UIImage总是零?我只是不知道这里发生了什么......

帮助....?

由于

2 个答案:

答案 0 :(得分:1)

imageWithContentsOfFile:的返回值为

  

指定的新图像对象   file,如果方法不能,则为nil   初始化图像   指定的文件。

您确定该位置的文件是有效的图像文件吗?

答案 1 :(得分:1)

在将图像传递给i

时尝试此操作
 i = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"001-1.png" ofType:@"png" inDirectory:@"word/001"];

快乐的编码......