UIImage 有2种加载图片的方式
- 有缓存
(图片以缓存的形式一直停留在程序中)可能会导致占用大量内存而使程序崩溃,加载少图时使用。
+(UIImage *)imageNamed:(NSString *)name;
name 是图片名称
2.无缓存(图片占用的内存会在一些特定操作后被清楚),加载大量图片时使用
+(UIImage *)imageWithContentsOfFile:(NSString *)path
-(id) initWithContentOfFile:(NSString *)path;
path是图片全路径
例如:
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:filename ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
在使用NSString *path = [bundle pathForResource:filename ofType:nil]; 获取路径为空,解决办法:
添加图片时勾选的选项选择如图所示,最终正常运行。