Programmatically access image assets

后端 未结 3 1280
小鲜肉
小鲜肉 2020-12-06 23:22

My image assets catalog has a folder named \"Nature\" with hundreds of image files inside.

I would like to get an array of image file names under the \"Nature\" dire

3条回答
  •  情歌与酒
    2020-12-07 00:09

    this may be late but this might helps others

    you can not directly access all the file from assets as its compiled to .car

    alternative to access the all files from the assets is to create the .plist file with the all images that assets contains and plist file can be easily accessible. following are the steps to achieve this

    Step 1: add script in Build Phases -> Run Script

    echo "
    
    
    " >  ${SRCROOT}/${PROJECT_NAME}/files.plist;
    ## Chnage Assets.xcassets if not using default one
    find ${SRCROOT}/${PROJECT_NAME}/Assets.xcassets -type f -not -iname "*@*" -name "*.png"  -exec basename {} \; | while read line; do echo \$line\\$line\; done >> ${SRCROOT}/${PROJECT_NAME}/files.plist;
    echo "" >>  ${SRCROOT}/${PROJECT_NAME}/files.plist;
    

    this script generate files.plist in project directory with all images in assets file add this file to project

    Step2: Accessing plist file

    NSURL *filepath = [[NSBundle mainBundle] URLForResource:@"files" withExtension:@"plist"];
    NSMutableDictionary *allXCAssetsImages = [NSMutableDictionary dictionaryWithContentsOfFile:filepath.path];
    

提交回复
热议问题