繁体   English   中英

如何从iOS中的NSMainBundle中读取图像

[英]How To Read Images From NSMainBundle in iOS

我的Xcode项目中具有以下结构。

在此处输入图片说明

我需要从项目中读取所有图像,并将其存储在数组中。

我从其他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);

如有任何帮助,我将不胜感激。

将所有图像放入一个数组很简单

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"];

您可以在下面尝试

NSArray *pathArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil];

[pathArray enumerateObjectsUsingBlock:^(NSString *pathString, NSUInteger idx, BOOL *stop) {

    UIImage *yourImage = [[UIImage alloc] initWithContentsOfFile:pathString];
}];

请使用此代码,希望它能工作。

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM