图像未从文档目录的子目录加载

时间:2010-12-29 09:44:14

标签: iphone objective-c cocoa-touch iphone-sdk-3.0 ios4

HI,

我正在从文档目录的子目录加载图像。这是代码。

 NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDir = [documentPaths objectAtIndex:0];

cell.imageView.image = [UIImage imageWithContentsOfFile:[documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"images/category/%@", element.image]]];

我的图片位于DocumentDir / images / category / img1.png,DocumentDir / images / category / img2.png,DocumentDir / images / category / img3.png .....

然而,当图像在DocumentDir时,它加载正常。但是当它在上面的子目录中时它不会加载。我在这里做错了吗?

谢谢

2 个答案:

答案 0 :(得分:1)

解决了这个问题。在“element.image”变量的末尾有一个新的行字符。

答案 1 :(得分:0)

NSString * docPath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@“images / category /%@”,element.image]];

cell.imageView.image = [UIImage imageWithContentsOfFile:docPath];

使用此功能,图片文件还应包含.png或.jpg。使用imageWithContentsOfFile方法时需要它。