How to get the path to the image form Resource of the main package

I have an iPhone application in which I added the images(Group) directory to Resources(group). I want to access the images that are in images. I need a path to these images, so I am creating the following code.

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"myImage1" ofType:@"png" inDirectory:@"images"];
NSLog(@"%@", imagePath);

Result:

(null)

I also tried this code

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"myImage1" ofType:@"png"];

But this code gives me the result (null).

myImage1 way Resources/images/myImage1.png

I do not know what the problem is. Please suggest some corrections.

+5
source share
3 answers

Adding an image with the following method helped solve my problem.

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

.

[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png"];
+8

.

: Target β†’ "Build Phases" β†’ "copy bundle Resources" .

RUN. .

....

NSBundle pathForResource inDirectory

, ...

NSString *path = [[NSBundle mainBundle] resourcePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = [[NSError alloc] init];
NSArray *directoryAndFileNames = [fm contentsOfDirectoryAtPath:path error:&error];

AndFileNames , ....

+6

First of all, check if there is a file with a name in the main package.

It gives null when there is no file with that name .

0
source

All Articles