2016-05-26 7 views
0

bgs.bundleには20個のイメージがあり、image_fileをロードしようとしています。iOSのリソースリターンnilのNSBundleパス

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私は正常に動作しているfileNameをも印刷されてきた、常にnullです。 私のコードで何が問題になることがありますか、バンドルからファイルをロードする他の方法がありますか?

+0

ofTypeにnilを設定する理由はありますか? –

+0

はい、バンドルには.pngと.jpgが含まれています – jpm

+0

はfilePathになりません。 –

答えて

2

主な問題は、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"]; 
+0

bgsBundleからパスを取得するには? – jpm

+0

'mainBundle'と同じ方法です。私の更新された答えを見てください。 – rmaddy

+0

落ち着きました。ありがとうございました! – jpm

関連する問題