Access images from nsbundle

I have a structure that is trying to access xib and images from a package. I can access the xib object, but could not load the images in the UIImageView. I even check if the file exists and the console output is yes ...

Any idea of ​​uploading an image?

Code for loading xib in the structure view control file

self = [super initWithNibName:@"ViewController_iPad" bundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"MyBundle" ofType:@"bundle"]]]; 

Image Upload Code

 - (void) loadImage { NSString *path = [[NSBundle mainBundle] pathForResource:@"MyBundle.bundle/test" ofType:@"png"]; BOOL present = NO; present = [[NSFileManager defaultManager] fileExistsAtPath:path]; if (present) { NSLog(@"MyBundle.bundle/test exists"); } else { NSLog(@"MyBundle.bundle/test does not exists"); } self.testImageView.image = [UIImage imageNamed:path]; self.testImageView2.image = [UIImage imageWithContentsOfFile:path]; } 

MyTestHarnessApp [11671: c07] MyBundle.bundle / test exists

+1
source share
5 answers

When you use imageNamed: you must pass in a simple file name, and the image must exist in the root directory of the application resource.

Your image is not in the root directory of the resource, it is in a subfolder.

Since you got the full image path in the path variable, you should use the UIImage imageWithContentsOfFile: method:

 self.testImageView.image = [UIImage imageWithContentsOfFile:path]; 
+4
source

If +[UIImage imageNamed:] does not resolve the path correctly, you can always use:

 self.testImageView.image = [UIImage imageWithContentsOfFile: path]; 
+2
source

You can only use the following:

 self.testImageView.image = [UIImage imageNamed:@"MyBundle.bundle/test.png"]; 

But it looks like if you put the XIB in the kit, the interface will refer to the package.

 self.testImageView.image = [UIImage imageNamed:@"test.png"] 
+1
source

try step by step.

First enter the set path within your main package:

 NSString *myBundlePath=[[NSBundle mainBundle] pathForResource:MyBundle ofType:@"bundle"]; 

Check for anything in the kit. This is more suitable for double checking. Once it works, you can omit this code. But it’s always good to check things.

  NSBundle *imageBundle=[NSBundle bundleWithPath: myBundlePath]; NSArray *allImageURLs=[imageBundle URLsForResourcesWithExtension:@"png" subdirectory:nil]; NSLog(@"imageURLS: %@",allImageURLS); 

Since you already have the path to the package, add the image name to the path. Or you can just pull it from the list of URLs that you already have in the above code.

 NSString *myImagePath=[myBundlePath stringByAppendingPathComponent:@"test.png"]; NSLog(@"myImagePath=%@",myImagePath); 

Then, since you have the full path, upload the image

 UIImage *testImage = [UIImage imageWithContentsOfFile:backgroundImagePath]; 

Code taken from the delivery application.

Luck

Tim

+1
source

If the images are already in the main set, just use the image name: test.png or just check

 [UIImage imageNamed:@"test.png"]; [UIImage imageNamed:@"test"]; 
0
source

Source: https://habr.com/ru/post/1385847/


All Articles