2014-03-24 26 views
0

我的文件名爲:landing_load1 @ 2x ... landing_load4 @ 2x在我的項目中。 (沒有非視網膜文件)NSBundle mainBundle pathForResource:不工作,除非@ 2x包括

此代碼:

for(int x=1; x < 5; x++){ 
    NSString * imageTitle = [NSString stringWithFormat:@"landing_load%[email protected]", x]; 
    UIImage * loadingImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageTitle ofType:@"png"]]; 

    [loadingImages addObject:loadingImage]; 
} 

但是,這並不工作:

for(int x=1; x < 5; x++){ 
    NSString * imageTitle = [NSString stringWithFormat:@"landing_load%i", x]; 
    UIImage * loadingImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageTitle ofType:nil]]; 

    [loadingImages addObject:loadingImage]; 
} 

而且這不起作用:

for(int x=1; x < 5; x++){ 
     NSString * imageTitle = [NSString stringWithFormat:@"landing_load%i", x]; 
     UIImage * loadingImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageTitle ofType:nil]]; 

    [loadingImages addObject:loadingImage]; 
} 

問題:我知道我不必顯式調用@ 2x或指定文件類型,所以任何想法爲什麼它不工作,除非我明確寫出整個fi列出來?

注:測試的iPad 4

回答

1

記錄

NSString * imageTitle = [NSString stringWithFormat:@"landing_load%i", x]; 

意味着你有一對圖形文件的任何資源 - 無論是「filename.png」和「[email protected]」。 ,或者,它可以被用來作爲「別名」只對「filename.png」 如果只有「[email protected]」,你有兩種可能的變體: 1.使用類似的東西

NSString * imageTitle = [NSString stringWithFormat:"landing_load%[email protected]", x]; 

或同時擁有任何資源的文件(「filename.png」和「[email protected]」)。

1

在「非」工作例如,你有沒有在ofType參數說明nil

UIImage * loadingImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]  pathForResource:imageTitle ofType:nil]]; 

變化png,因爲你正在使用

-[NSBundle pathForResource:ofType:] 

方法應該做的伎倆:)

+1

更新了問題。除非添加@ 2x,否則仍然不起作用。謝謝 – user3456115

0

,你需要指定第二個參數,你被設置爲「資源類型PNG「你說它的工作原理。

0

@ 2x是作爲[UIImage imageNamed:]的一部分處理的,而不是作爲[NSBundle pathForResource:ofType:]的一部分。只要使用前者,你應該很好。

+1

imageNamed不釋放圖像並導致內存問題 – hasan83

相關問題