以编程方式访问图像资产 [英] Programmatically access image assets

查看:91
本文介绍了以编程方式访问图像资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图像资源目录有一个名为自然的文件夹,里面有数百个图像文件。

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

我想获得一个图像文件名列下的自然我的图像资产中的目录。然后,我将从此数组中选择一个随机图像名称,并使用 [UIImage imageNamed:@...] 显示它。我被困在如何在'IncludedStudyImages'目录下获取一系列图像名称。我甚至无法在文档中找到有关如何以编程方式与我的图像资源进行交互的任何内容(除了显示它们)。

I would like to get an array of image file names under the "Nature" directory in my image assets. I will then choose a random image name from this array and display it with [UIImage imageNamed:@"..."]. I am stuck on how to get an array of image names under the 'IncludedStudyImages' directory. I cannot even find anything in the documentation on how to interact with my image assets programmatically (other than displaying them).

感谢您的帮助。

推荐答案

这可能会迟到但这可能有助于其他人

this may be late but this might helps others

你不能直接从资产中访问所有文件,因为它编译为.car

替代访问资产中的所有文件是使用all创建.plist文件可以轻松访问资产包含和plist文件的图像。以下是实现此目的的步骤

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

步骤1:在构建阶段添加脚本 - >运行脚本

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

echo "<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>" >  ${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 \<key\>$line\</key\>\<string\>$line\</string\>; done >> ${SRCROOT}/${PROJECT_NAME}/files.plist;
echo "</dict></plist>" >>  ${SRCROOT}/${PROJECT_NAME}/files.plist;




此脚本生成 files.plist 项目目录包含资产文件中的所有
图像将此文件添加到项目

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

第2步:访问plist文件

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

这篇关于以编程方式访问图像资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆