How to get resource path using subpath Using NSBundle

Create a folder structure like

*Resources/books/CD_en/icon/0.jpg; *Resources/books/CD_en/icon/1.jpg; *Resources/books/DD_en/icon/0.jpg; *Resources/books/DD_en/icon/1.jpg; 

how to get the resource path from NSBundle using the subdirectory 'books/CD_en/icon/0.jpg'

I tried to use

 - (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension inDirectory:(NSString *)subpath [[NSBundle mainBundle] pathForResource:@"0.jpg" ofType:@"jpg" inDirectory:@"books/CD_en/icon"]; 

but returns nil .

+7
objective-c iphone
source share
2 answers

I understood. We just need to add the folder structure "books / ..." to the application resource folder. Which will be displayed in blue.

Then the existing code works successfully.

Thanks yehnan.

+2
source share

I had the same issue to retrieve an image from a Resource folder. Adding an image using the following method solved my problem.

  • Right-click on the project in xcode and select Add Files to "project name" .
  • Select an image file from the local computer disk.
  • In the pop-up window, select Copy items into destination group folder and Create Folder References for any added folders and Add to targets .
  • click Add .

Then with the following code I can extract my image.

 [[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png"]; 
+1
source share

All Articles