I'm fairly new to iOS development and cordova and I'm a little confused by Xcode and objective-C in terms of how to use an NSUrl or NSBundle to access files. What I want to achieve is to access all of the .png
files from inside www/images
from my app/Classes/MainViewController.m
and programmatically create a new Image View for each one.
My file structure looks like this
App Folder >
> .xcodeproj file
> xcode app folder > Classes > MainViewController.m
> www > images
I can handle the instantiation of the image views but I'm confused about how I should be obtaining the images. Is using an NSBundle appropriate in this situation and how can I use an NSURL to navigate from my MainViewController to my images location?
Thanks.
You can obtain apps root directory and then add path to your image file:
NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSString *imagePath = [appFolderPath stringByAppendingString:@"/www/images/some_image.png"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
But in general if you are trying to show image from native side, then it is better to put images into Resources folder (using "resource-file" attribute in your plugin.xml) with @2x and @3x postfixes, so iOS will load you appropriate image according to current device.
Plugin.xml example:
...
<platform name="ios">
...
<resource-file src="icons/some_image@2x.png" />
<resource-file src="icons/some_image@3x.png" />
...
</platform>
...
And then in Objective-C:
UIImage *image = [UIImage imageNamed:@"some_image"];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.