gpt4 book ai didi

ios - iOS 资源返回 nil 的 NSBundle 路径

转载 作者:行者123 更新时间:2023-11-28 21:27:37 24 4
gpt4 key购买 nike

我有一个包含 20 张图像的 bgs.bundle,我正在尝试加载 image_file。

NSString *bpath = [[NSBundle mainBundle] pathForResource:@"bgs" ofType:@"bundle"];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bpath error:NULL];
for (NSString *fileName in files) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"nil"];
if (filePath){
NSLog(filePath);
}else{
NSLog(@"file path is null");
}

filePath 始终为 null,我还打印了 fileName,它工作正常。我的代码可能有什么问题,或者有其他方法可以从包中加载文件?

最佳答案

您的主要问题是您试图从主包而不是 bgs.bundle 加载 fileName

像这样更改您的代码:

NSString *bpath = [[NSBundle mainBundle] pathForResource:@"bgs" ofType:@"bundle"];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bpath error:NULL];
for (NSString *fileName in files) {
NSString *filePath = [bpath stringByAppendingPathComponent:fileName];
NSLog(@"filePath = %@", filePath);
}

另一种选择是为 bgs.bundle 获取一个 NSBundle

NSString *bpath = [[NSBundle mainBundle] pathForResource:@"bgs" ofType:@"bundle"];
NSBundle bgsBundle = [NSBundle bundleWithPath:bpath];

现在您可以根据需要使用bgsBundle。示例:

NSString *filePath = [bgsBundle pathForResource:@"someImage" ofType:@"png"];

关于ios - iOS 资源返回 nil 的 NSBundle 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37469569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com