enum image resources in monotouch - ios

is there a way to enum programmatically all image resources in a folder?
I want to automate animation images sequence creation process and load images like this:
var images = EnumResouceImages("Images/");
foreach(imgPath in images)
UIImage myImg = UIImage.FromFile(imgPath);

Once you know your application's root directory, e.g. by calling NSBundle.MainBundle.BundleUrl.Path, you can do this using the System.IO types that the base class library (BCL) provides.
E.g. this code:
foreach (var filename in Directory.EnumerateFiles (Path.Combine (NSBundle.MainBundle.BundleUrl.Path, "Images"), "*.png"))
Console.WriteLine (filename);
will print every .png file located in your application's Images/ directory.
You can change the filter (e.g. for jpegs or other formats), the directory name (Images) or the processing (Console.WriteLine) with your own logic.

Related

Render Images in document.directory in a markdown file in swift(SwiftyMarkdown)

Am using SwiftyMarkdown framework to render my .md file to have dynamic formatted data in my VC. But i am not able to load images in my document directory referenced in .md file. I can load images if they are available in Bundle.
Any other option or framework to fix this issue? I want to dynamically load images from server and save them documents directory and refer them in .md file which is also saved in same documents directory/folder. So the url of image and .md file is same.
"MyFile.md" in documents directory contains below content
Continue your walk, joining up with your original perimeter path of the item. "
In my VC viewdidload, i access the url path and load nsattributed string to my textview using SwiftyMarkdown frmaework
let url = self.getDocumentsDirectory().appendingPathComponent("MyFile.md")
if let md = SwiftyMarkdown(url: url) {
textView.attributedText = md.attributedString()
}
The formatted text is loaded but no image as its not available in my Bundle. But its saved in the same document directory as that of "MyFile.md"
Hope its clear!

List of files in flutter

I have images and I want to load them to my appplication.
my_app/lib/... - here my source files
my_app/assets/images/... - here my images
I need to get list of files in assets/images/ and after that show some of them (according to other logic)
I'm trying this
final dir = Directory("assets/images/");
print(dir.existsSync()); // <---- it also print: false
var files = dir.listSync().toList();
files.forEach((e) => list.add(MyImageItem(e.path)));
The problem is: I recieve exception
FileSystemException: Directory listing failed, path = 'assets/images/'
(OS Error: No such file or directory, errno = 2)
I've tried different ways: assets/images/, images/, images and so on
My pubspec.yaml
flutter:
assets:
- assets/images/
- assets/
When I create Image directly all is fine
new Image(image: AssetImage("assets/images/cat.png"))
I knew that previously (month ago) each resource has to be declared in pubspec.yaml directly, but now assets/images/ is ok.
I can load file by direct path. Why I can't access a directory? How to get list of files in directory to get them from my code?
When you add files to your assets, it means that you already know their paths.
Store all the images paths in a list and access them whenever you need.
For example if you have 3 images in
your_app/assets/images/image1.jpg
your_app/assets/images/image2.jpg
your_app/assets/images/image3.jpg
create a list and store all of them like:
List<String> imagePaths = ['assets/images/image1.jpg', 'assets/images/image2.jpg', 'assets/images/image3.jpg'];
and for example if you want to access your first image, use
AssetImage(imagePaths[0])
I keep a json file inside assets which records the file tree of assets folder.
When file list is needed, I just read from the json file.
Such json file can be easily generated by code.
However, this does not solve your problem directly when u have 5-10k images.
Only one json file might be too large to read.
Images should be grouped and recorded in separated json files.

How to access a list of all files in resource folder of my project?

I have some .TXT files saved in Resource folder of my project. I want to display a list to user in which all files from resource folder are shown and user can select the file he desires.
Later on i will read the user selected file and show it on screen.
Take a look at the NSBundle function pathsForResourcesOfType:inDirectory: That will give you a list of the paths to all the files in a sub-bundle of a bundle. if you call that method on the main bundle you'll get a list of all the files of a certain type in a sub-directory of the main bundle.
(I have no idea how to make use of these functions from xamarin.)
This gets FileInfo's on all txt files in the resources:
var fileInfos = NSBundle.GetPathsForResources(".txt", path)
.Select(a => new FileInfo(a));
Now you have the short name, full name etc to play with:
foreach (var fileInfo in fileInfos)
{
System.Diagnostics.Debug.WriteLine(fileInfo.Name);
using (var streamReader = new StreamReader(new FileStream(fileInfo.FullName, FileMode.Open)))
{
System.Diagnostics.Debug.WriteLine(streamReader.ReadToEnd());
}
}

Opencv Reading Multiple images from Multiple folder

I have a folder which contain 100 or more sub folders and each of them contain many images.(Caltech Database)
I have problem with how to read them from the different folders ?
I want to store them as a single matrix with stacking columms of each image.
Use Boost Filesystem for C++. You can load all files in a directory and pass the file location to OpenCV in a string.
string folder = "../images/";
vector<string> imageFileLocations;
namespace fs = boost::filesystem;
vec v;
copy(fs::directory_iterator(folder), fs::directory_iterator(), back_inserter(v));
sort(v.begin(), v.end());
for (vec::const_iterator it(v.begin()); it != v.end(); ++it) {
if (fs::is_regular_file(*it)) {
string location = it->string();
imageFileLocations.push_back(location);
}
}
You'll have to add something recursive to be able to go into other folders. You can do that by checking if the iteraotr is at a file or a folder. See the Boost website for examples.

read file from res folder blackberry

I want to read file from "res" folder on blackberry. The file that i used is a file javascript.
I used this code InputStream in = classs.getResourceAsStream("file.js");. But i get "could not find this path" and I use also
String srcFile = "/res/ressourcesWeb/file.js";
FileConnection srcConn = (FileConnection) Connector.open(srcFile, Connector.READ);
InputStream in = srcConn.openInputStream();
but i got an exception.
Can any one help me to read the file and give me the right path that should I use?
Your res folder has to be inside src folder to be accessed from your code.
src folder is the root folder of your project package. And all folders outside of src folder are invisible for the code at runtime.
Check this post for more details: Blackberry runtime error: FRIDG: could not find img/logo.png
There's file location principle described.
You actually do not need to put your resources under the src folder for them to be accessible from your code.
That is one way to do it, but I don't think it's the best way. Files under the src folder should really be source code, not images, or other resources. For JavaScript resources, it's debatable whether those should be under src or not. Most projects I've seen have used the src folder for only Java source code.
In any case, if you would like to keep your file (or other resources, like images) outside the src folder, you can do so. The BlackBerry plugin for Eclipse actually sets it up like this by default, when you create a new project. There is a res folder at the top level, next to (not under) src.
If you have
src\
src\com\mycompany\myapp\
res\
res\resourcesWeb\
res\resourcesWeb\file.js
Then, you can open the file like this:
String jsPath = "/resourcesWeb/file.js";
InputStream input = getClass().getResourceAsStream(jsPath);
byte [] content = IOUtilities.streamToBytes(input);
String contentAsString = new String(content);
P.S. You also can probably do this:
String jsPath = "/file.js";
InputStream input = getClass().getResourceAsStream(jsPath);
and not specify the path to the resource. Obviously, this will only work if there are no naming conflicts in your resource folders (e.g. you don't have /res/resourcesWeb/file.js and also /res/otherPath/file.js)

Resources