How to get path of image form Resource of main bundle

試著忘記壹切 提交于 2019-12-05 05:06:44

Adding image by following method solved my problem.

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

Then with bellow code i can retrieve my image.

For Objective-C version

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

For Swift 4.0 version

Bundle.main.path(forResource: "imageName", ofType: "png")

for soultion....

Go to : Target -> "Build Phases" -> "copy bundle Resources" Then add that particular file here.

clean the project and RUN. It works.

and also see...

NSBundle pathForResource inDirectory gives nil

and also check...

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

print all value of this directoryAndFileNames and check your filename is exist in that or not....

First of all Check if there is a file with name name in the main Bundle.

It gives null when there is no file with such a name.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!