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

来源:互联网 发布:网络彩票能赚钱吗 编辑:程序博客网 时间:2024/05/16 09:39

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

NSArray *ary = [[NSFileManagerdefaultManager] contentsOfDirectoryAtPath:[[NSBundlemainBundle] pathForResource:@"folderName" ofType:nil]error:nil];

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

for(NSString *imageNamein ary)

{

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

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

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

[view addSubView:imageView];

}

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

NSString *path = [[NSBundlemainBundle] resourcePath];

        

        path = [pathstringByAppendingPathComponent:@"folderName"];

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

       UIImage *image = [UIImageimageWithContentsOfFile:path];

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

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



原创粉丝点击