我是iOS开发和cordova的新手,我对Xcode和objective-C在如何使用NSUrl或NSBundle访问文件方面有点困惑。我想要实现的是从我的.png
访问www/images
内的所有app/Classes/MainViewController.m
文件,并以编程方式为每个文件创建一个新的图像视图。
我的文件结构如下所示
App Folder >
> .xcodeproj file
> xcode app folder > Classes > MainViewController.m
> www > images
我可以处理图像视图的实例化,但我对如何获取图像感到困惑。在这种情况下是否使用适合的NSBundle,如何使用NSURL从我的MainViewController导航到我的图像位置?
感谢。
答案 0 :(得分:1)
您可以获取应用程序根目录,然后添加路径到您的图像文件:
NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSString *imagePath = [appFolderPath stringByAppendingString:@"/www/images/some_image.png"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
但总的来说,如果你试图从原生方面展示图像,那么最好将图像放入Resources文件夹(使用"资源文件"属性在plugin.xml中)和@ 2x并且@ 3x postfixes,因此iOS会根据当前设备为您加载适当的图像。
Plugin.xml示例:
...
<platform name="ios">
...
<resource-file src="icons/some_image@2x.png" />
<resource-file src="icons/some_image@3x.png" />
...
</platform>
...
然后在Objective-C中:
UIImage *image = [UIImage imageNamed:@"some_image"];