获取以Resource文件夹中的前缀开头的所有文件名

时间:2022-04-09 00:30:12

How can we get all file names starting with a prefix from resource folder..

我们如何从资源文件夹中获取以前缀开头的所有文件名。

2 个解决方案

#1


21  

You can achieve that adapting the following code:

您可以实现以下代码:

NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
                  [[NSBundle mainBundle] bundlePath] error:nil];
NSArray *pngs = [files filteredArrayUsingPredicate:
                 [NSPredicate predicateWithFormat:@"self ENDSWITH[cd] '.png'"]];
NSLog(@"%@", pngs);

Running this code you will see the list of PNG's on your bundle. Change the predicate to match with the prefix you want:

运行此代码,您将看到捆绑包上的PNG列表。更改谓词以匹配所需的前缀:

NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
                  [[NSBundle mainBundle] bundlePath] error:nil];
NSArray *filesWithSelectedPrefix = [files filteredArrayUsingPredicate:
                                    [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] 'prefix'"]];
NSLog(@"%@", filesWithSelectedPrefix);

#2


1  

Updated for Swift 4.0:

针对Swift 4.0进行了更新:

In my case, I was looking for files that began with fileName-1.png where there might be other files with the fileName prefix.

就我而言,我一直在寻找以fileName-1.png开头的文件,其中可能有其他带有fileName前缀的文件。

1) I thought about using regex, but splitting the string was faster and easier, so I went that route to get the prefix.

1)我考虑使用正则表达式,但拆分字符串更快更容易,所以我去那条路线获取前缀。

let myKnownFileNameString = "fileName-1.png"
let filePrefix = myKnownFileNameString.split(separator: "-").first

2) Create an empty array to store images you find:

2)创建一个空数组来存储您找到的图像:

var imageArray: [UIImage] = []

3) Then, get the URLs from the bundle, filter them and map the URLs to UIImage instances:

3)然后,从包中获取URL,过滤它们并将URL映射到UIImage实例:

if let filePrefix = filePrefix, let fileURLs = Bundle.main.urls(forResourcesWithExtension: "png", subdirectory: nil) {
    imageArray = fileURLs.filter{ $0.lastPathComponent.range(of: "^\(filePrefix)", options: [.regularExpression, .caseInsensitive, .diacriticInsensitive]) != nil }
                         .map{ UIImage(contentsOfFile: $0.path)! }
}

From there, you've got an array of images that you can do stuff with (animate them in a UIImageView, etc.)

从那里,你有一系列你可以做的东西(在UIImageView中制作它们等等)

#1


21  

You can achieve that adapting the following code:

您可以实现以下代码:

NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
                  [[NSBundle mainBundle] bundlePath] error:nil];
NSArray *pngs = [files filteredArrayUsingPredicate:
                 [NSPredicate predicateWithFormat:@"self ENDSWITH[cd] '.png'"]];
NSLog(@"%@", pngs);

Running this code you will see the list of PNG's on your bundle. Change the predicate to match with the prefix you want:

运行此代码,您将看到捆绑包上的PNG列表。更改谓词以匹配所需的前缀:

NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
                  [[NSBundle mainBundle] bundlePath] error:nil];
NSArray *filesWithSelectedPrefix = [files filteredArrayUsingPredicate:
                                    [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] 'prefix'"]];
NSLog(@"%@", filesWithSelectedPrefix);

#2


1  

Updated for Swift 4.0:

针对Swift 4.0进行了更新:

In my case, I was looking for files that began with fileName-1.png where there might be other files with the fileName prefix.

就我而言,我一直在寻找以fileName-1.png开头的文件,其中可能有其他带有fileName前缀的文件。

1) I thought about using regex, but splitting the string was faster and easier, so I went that route to get the prefix.

1)我考虑使用正则表达式,但拆分字符串更快更容易,所以我去那条路线获取前缀。

let myKnownFileNameString = "fileName-1.png"
let filePrefix = myKnownFileNameString.split(separator: "-").first

2) Create an empty array to store images you find:

2)创建一个空数组来存储您找到的图像:

var imageArray: [UIImage] = []

3) Then, get the URLs from the bundle, filter them and map the URLs to UIImage instances:

3)然后,从包中获取URL,过滤它们并将URL映射到UIImage实例:

if let filePrefix = filePrefix, let fileURLs = Bundle.main.urls(forResourcesWithExtension: "png", subdirectory: nil) {
    imageArray = fileURLs.filter{ $0.lastPathComponent.range(of: "^\(filePrefix)", options: [.regularExpression, .caseInsensitive, .diacriticInsensitive]) != nil }
                         .map{ UIImage(contentsOfFile: $0.path)! }
}

From there, you've got an array of images that you can do stuff with (animate them in a UIImageView, etc.)

从那里,你有一系列你可以做的东西(在UIImageView中制作它们等等)