How to Access All Images in a .Xcassets at Once

Can I access all images in a .xcassets at once?

Images.xcassets copies all the images in bundle, and it mixes up with other images, better follow the instructions in image below

Sample Image

By adding the folder with the option Create folder reference will actually create a folder in main bundle with its content, and all your images will be separated. And using the following code you can access all the images.

NSString *dirPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"images"];
NSError * error;
NSArray * images = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:dirPath error:&error];

Hope it helps.

Can I access an xcassets directory on the filesystem?

If there are other solutions to this problem, I'm open to that

The problem is that there is no introspection at runtime into an asset catalog: it isn't a "thing" you can "see" as far as Objective-C and Cocoa Touch are concerned.

The usual solution to this kind of problem is to drag a folder full of images into your project at the outset, and when you do, choose "Create folder references for any added folders" in the dialog - not "Create groups for any added folders". The result is that the folder is copied into your app bundle, and now you can use ordinary file system methods to say "every file in this folder".

How can I get all image names in asset catalog group?

So (following matt's suggestion) here is what I did:

I created a RunScript build phase to run before compiling

Sample Image

and used a script to create a txt file with the names of the files that I can then use during runtime

#!/bin/sh
>./your_app_folder/fileNames.txt
for FILE in ./your_app_folder/Images.xcassets/actions/*; do
echo $FILE >> ./your_app_folder/fileNames.txt
done

Get number of images in xcasset

There isn't a real easy way to go about it since the files are placed in the app at compile time. You can, though, create a RunScript in the Build Phases to be ran before compiling that creates a text file of the images. Then you can read in that text file and populate an array of images. You can use that array to get the details you need.

RunScript in the Build Phases:

#!/bin/sh
>./your_app_folder/fileNames.txt
for FILE in ./your_app_folder/Images.xcassets/actions/*; do
echo $FILE >> ./your_app_folder/fileNames.txt
done

Also, here's a link to an accepted answer that a previous poster asked with more details.

Another option if you do not want to deal with Build Phases, and one I'd probably choose, is not to use the Assets.xcassets file. Just place the images in a folder within your app bundle. It's much easier to read from the bundle.

UPDATE:

For the second option, you can drag your images (or folder of images) to your project, but be sure copy as a folder reference rather than group. Then in your code, you can read that folder using the FileManager.

let fileManager = FileManager.default
let imagePath = Bundle.main.resourcePath! + "/your_new_folder"
let imageNames = try! fileManager.contentsOfDirectory(atPath: imagePath)

imageNames in this case will be an array of file names within that url path. If go this route, you probably have to load the images by contentsOfFile too.

if let imagePath = bundle.path(forResource: fileName, ofType: nil) {
myImage = UIImage(contentsOfFile: imagePath)
}

How to get Assets.xcassets file names in an Array (or some data structure?)

Assets.xcassets is not a folder but an archive containing all the images using Assets.car as its filename.

If you really want to read the assets file then you need to use some library that can extract the contents of the file like this one.

Or you can create a bundle in your project and drag all the images you have there. In my case, I have Images.bundle in my project. To get the filenames you can do the following:

let fileManager = NSFileManager.defaultManager()
let bundleURL = NSBundle.mainBundle().bundleURL
let assetURL = bundleURL.URLByAppendingPathComponent("Images.bundle")
let contents = try! fileManager.contentsOfDirectoryAtURL(assetURL, includingPropertiesForKeys: [NSURLNameKey, NSURLIsDirectoryKey], options: .SkipsHiddenFiles)

for item in contents
{
print(item.lastPathComponent)
}

SWIFT 3/4 Version:

let fileManager = FileManager.default
let bundleURL = Bundle.main.bundleURL
let assetURL = bundleURL.appendingPathComponent("Images.bundle")

do {
let contents = try fileManager.contentsOfDirectory(at: assetURL, includingPropertiesForKeys: [URLResourceKey.nameKey, URLResourceKey.isDirectoryKey], options: .skipsHiddenFiles)

for item in contents
{
print(item.lastPathComponent)
}
}
catch let error as NSError {
print(error)
}

How to find specific images which start with a string in Swift Assets.xcassets

Assets folder cannot be accessed this way. One way to do that is creating a folder with all the images on your computer, and drag into the Xcode project. Dont forget to select the "Create folder references for any added folders" option. With that referenced folder, you can access all images:

guard let _resourcePath = Bundle.main.resourcePath else{
return
}
do{
if let url = NSURL(string: _resourcePath)?.appendingPathComponent("YOUR FOLDER NAME"){
let resourcesContent = try FileManager().contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)

for imageUrl in resourcesContent {
let imageName = imageUrl.lastPathComponent
print(imageName)
//CHECK IMAGE NAME STRING
}
}
}catch let error{
print(error.localizedDescription)
}


Related Topics



Leave a reply



Submit