iOS图片存放方式

2019-11-03  本文已影响0人  若水water

一、image.xcassets

NSString *path = [NSBundle.mainBundle pathForResource:@"xxx" type:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
#define STBundle_Name @"SafeToolResource.bundle"
#define STBundel_path  [[[NSBundle mainBundle ] resouecePath] stringByAppendingPathComponent:STBundle_Name]
#define STBundle  [NSBundle bundleWithPath:STBundel_path]
//使用
UIImageView *headerBgImgView = [[UIImageView alloc] init];
headerBgImgView.image = [UIImage imageWithContentsOfFile:[STBundle pathForResource:@"xxx" ofType:@"pne"]];

2、对UIImage 进行扩展,创建UIImage+BSKResources 类

// UIImage + BSKResources.h
@interface UIImage (BSKResources)
+ (UIImage *)bskImageNamed:(NSString *)imageName InBundleName:(NSString *)bundleName;
@end
/// UIImage+BSKResources.m
#import "UIImage+BSKResources.h"
@implementation UIImage (BSKResources)

+ (UIImage *)bskImageNamed:(NSString *)imageName InBundleName:(NSString *)bundleName
{
        NSString *resourcePath =  [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:bundleName];
        NSBundle *resourceBundle = [NSBundle bundleWithPath:resourcePath];
        return [UIImage imageNamed:imageName inBundle: resourceBundle compatibleWithTraitCollection: nil];
}
@end
//使用
UIImageView * headerBgImgView = [[UIImageView alloc] init];
headerBgImgView.image = [UIImage bskImageNamed:@"xxx" InBundleName:@"BSKResources.bundle"]];

四、Bundle 和 xcassets 的区别

上一篇 下一篇

猜你喜欢

热点阅读