添加图片的时候,可以拖到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:
方法加载图片不会产生缓存,适合用来加载使用次数较少的大图片。
网友评论