image.xcassets
图片直接加入工程中作为resource
读取方式:创建图片 resource 文件夹,直接将图片加入到工程中,使用如下方式读取图片
nsstring *path = [nsbundle.mainbundle pathforresource:@"xxx" type:@"png"]; uiimage *image = [uiimage imagewithcontentsoffile:path];
特性:在 resource 的图片管理方式中, 所有的图片创建都是通过读取文件数据得到的, 读取一次文件数据就会产生一次 nsdata 以及产生一个 uiimage。 当图片创建好后销毁对应的 nsdata,当 uiimage 的引用计数器变为 0 的时候自动销毁 uiimage,这样的话就可以保证图片不会长期地存在在内存中
使用场景:由于这种方法的特性, 所以 resource 的方法一般用在图片数据很大, 图片一般不需要多次使用的情况,比如说引导页背景(图片全屏)
优势:图片不会长期保存在内存当中, 所以不会有很多的内存浪费。同时, 大图一般不会长期使用, 而且大图占用内存一般比小图多了好多倍, 所以在减少大图的内存占用中, resource 做的非常好
使用bundle文件
使用 imagewithcontentsoffile 读取
/// bskdefine.h // bundle path #define stbundle_name @"safetoolresource.bundle" #define stbundle_path [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:stbundle_name] #define stbundle [nsbundle bundlewithpath:stbundle_path]
/// usage #import "bskdefine.h" uiimageview * headerbgimgview = [[uiimageview alloc] init]; headerbgimgview.image = [uiimage imagewithcontentsoffile:[seckill_bundle pathforresource:@"xxxx" oftype:@"png"]];
对 uiimage 进行扩展,创建 uiimage+bskresources 类
/// uiimage+bskresources.h ns_assume_nonnull_begin @interface uiimage (bskresources) + (uiimage *)bskimagenamed:(nsstring *)imagename inbundlename:(nsstring *)bundlename; @end ns_assume_nonnull_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
/// usage #import "uiimage+bskresources.h" uiimageview * headerbgimgview = [[uiimageview alloc] init]; headerbgimgview.image = [uiimage bskimagenamed:@"xxx" inbundlename:@"bskresources.bundle"]];
bundle 和 xcassets 区别
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
HDU - 5532 A - Almost Sorted Array
react native 实现扫描条形码功能(react-native-camera)
网友评论