4.读取和使用文件。
UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 50, 50)];
la.text = @"陈凯";
NSString *image_url = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"nono.bundle/images/Android.png"];
la.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:image_url]];

此时,因为以前我们获取UIImage对象大部分都是直接使用的api为:
[UIImageimageNamed:@"android.png"]
方法其实也是通过相对路径给我们找到了图片资源。
但是在我们自己制作的bundle文件夹的图片资源则不能使用该方法来找到了。
我们每次指定图片需要传入一个很长的地址,觉得比较麻烦。但是我们会发现,图片都是在xxx.bundle/images下面。
那么我们也可以使用宏或是类别来定义一个方法来简单到和[UIImageimageNamed:@"android.png"]一样。只需一个文件名就行了。
宏的表现可以参考three20下的
TTIMAGE(@"bundle://Three20.bundle/images/backIcon.png")
返回的就是一个image对象。
three20框架中其实是使用了scheme的思想,bundle://和document;//来加载不同文件夹下的资源。
加载自定义bundle文件夹下的图片最后就是根据
先获取应用的绝对路径
mainbundle_path = [NSBundlemainBundle] resourcePath] ;
然后截取了bundle://后面的path 和mainbundle_path组装成最后的地址。
对于我们,简单的实现可以直接使用类别
-(UIImage*) imagesNamedFromCustomBundle:(NSString *)name
{
NSString *main_images_dir_path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"nono.bundle/images"];
// NSAssert(main_images_dir_path, @"main_images_dir_path is null");
NSString *image_path = [main_images_dir_path stringByAppendingPathComponent:name];
return [UIImage imageWithContentsOfFile:image_path];
}
创建一个UIImage的类别,添加这个扩展的方法。
----end---
