批量加载图片资源时模拟器的显示而真机不显示的小问题

在IOS开发中,有时候需要批量加载一个文件夹的所有资源,这时候会用到

 

NSArray *ary = [[NSFileManager defaultManagercontentsOfDirectoryAtPath:[[NSBundlemainBundle]pathForResource:@"folderName" ofType:nilerror:nil];

这个可以获得所有的文件夹下的文件名数组,如果是一堆图片时,可能你会粗心的这样调用

for(NSString *imageName in ary)

{

UIImage *image = [[UIImage alloc] imageName:imageName];

//IMAGE进行操作,运行后模拟器上会显示出来图片,但是真机上就不行了

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

[view addSubView:imageView];

}

 

正确的操作应该是这样的:

 

NSString *path = [[NSBundle mainBundleresourcePath];

       

       path = [path stringByAppendingPathComponent:@"folderName"];

       path = [path stringByAppendingPathComponent:[ary objectAtIndex:i]];

       UIImage *image = [UIImage imageWithContentsOfFile:path];

//这时候真机上就没问题了

这种读取文件的低级错误有时候会让人头痛老半天,希望不会有和俺一样的朋友犯这样的错误,呵呵...

posted @ 2013-02-22 19:00  追风.扬  阅读(215)  评论(0)    收藏  举报