[英]How To Read Images From NSMainBundle in iOS
I have the structure like the following in my Xcode project. 我的Xcode项目中具有以下结构。
I need to read all the images from project and need to store it in array. 我需要从项目中读取所有图像,并将其存储在数组中。
I have used the following code from some other StackOverflow posts, but it's not working. 我从其他StackOverflow帖子中使用了以下代码,但无法正常工作。
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Images"ofType:@""];
NSDirectoryEnumerator *direnum;
direnum = [fileManager enumeratorAtPath: filePath];
imageFolder = [NSMutableArray new];
for(NSString *filename in direnum){
if([[filename pathExtension] isEqualToString:@"png"]){
[imageFolder addObject:filename];
}
}
NSLog(@"Files in the folder %@",imageFolder);
I would appreciate, if any one help me to complete this. 如有任何帮助,我将不胜感激。
it's simple to get all the images into a array 将所有图像放入一个数组很简单
NSBundle *imageBundle=[NSBundle bundleWithPath: myBundlePath];
NSArray *allImageURLs=[imageBundle URLsForResourcesWithExtension:@"png" subdirectory:nil];
- (NSArray *)pathsForResourcesOfType:(NSString *)extension inDirectory:(NSString *)subpath
将为您提供所需的数组。
NSArray *imagesArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"Images"];
You can try below 您可以在下面尝试
NSArray *pathArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil];
[pathArray enumerateObjectsUsingBlock:^(NSString *pathString, NSUInteger idx, BOOL *stop) {
UIImage *yourImage = [[UIImage alloc] initWithContentsOfFile:pathString];
}];
Please use this code, hope it will work. 请使用此代码,希望它能工作。
NSString *dirPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"images"];
NSError * error;
NSArray * images = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:dirPath error:&error];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.