iOS之图片资源

添加图片的时候,可以拖到Assets.xcassets中,也可以添加到bundle中。

1.添加到Assets.xcassets中:

运行的时候,图片会被打包成Assets.car文件,下载API包,是无法拿到图片资源的
iOS8.0之后,.jpg.png都可以添加到Assets.xcassets

//    在Assets.xcassets中的图片,只能用imageNamed 方法获取到
UIImage *image = [UIImage imageNamed:@"background"];
2.添加到bundle中:
UIImage *image1 = [UIImage imageNamed:@"background1.jpg"];

//获取图片在mainBundle中的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"background2" ofType:@"jpg"];
UIImage *image2 = [UIImage imageWithContentsOfFile:path];


//    获取在自定义bundle中的图片路径
//    获取自定义bundle的路径
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"customBundle" ofType:@".bundle"];
NSBundle *customBundle = [NSBundle bundleWithPath:bundlePath];
NSString *imgPath = [customBundle pathForResource:@"background3" ofType:@".jpg"];
UIImage *image3 = [UIImage imageWithContentsOfFile:imgPath];

使用imageNamed:方法加载图片,图片会有缓存,适合用来加载使用频繁的小图片,imageWithContentsOfFile:方法加载图片不会产生缓存,适合用来加载使用次数较少的大图片。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容