UIImage可以通过两种方式初始化
NSString *name = [NSString stringWithFormat:@"文件名.jpg"];
NSString *path = [[NSBundle mainBundle] pathForResoure:name ofType:nil];
//第一种初始化方式
UIImage *img = [[UIImage alloc] initWithContentsOfFile:path];
//第二种初始化方式
UIImage *img = [UIImage imageNamed:name];
两种初始化方式的区别:
使用UIImage *img = [[UIImage alloc] initWithContentsOfFile:path]; 初始化时,图片不放入缓存中。
使用UIImage *img = [UIImage imageNamed:name];初始化,图片会放在缓存中,程序关闭才会释放。
所以如果图片是经常使用并且占用内存不是太大的,使用第二种方法。如果图片占用内存比较大,则应该使用第一种方法,避免图片占用的缓存越来越多导致程序崩溃。
以上内容出自传智ios教学视频