我对 iOS 开发和 cordova 相当陌生,并且在如何使用 NSUrl 或 NSBundle 访问文件方面对 Xcode 和 Objective-C 有点困惑。我想要实现的是从我的 app/Classes/MainViewController.m
访问 www/images
内部的所有 .png
文件以编程方式为每个图像创建一个新的 ImageView 。
我的文件结构是这样的
App Folder >
> .xcodeproj file
> xcode app folder > Classes > MainViewController.m
> www > images
我可以处理 ImageView 的实例化,但我对应该如何获取图像感到困惑。在这种情况下使用 NSBundle 是否合适?我如何使用 NSURL 从我的 MainViewController 导航到我的图像位置?
谢谢。
最佳答案
您可以获取应用程序根目录,然后将路径添加到您的图像文件:
NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSString *imagePath = [appFolderPath stringByAppendingString:@"/www/images/some_image.png"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
但一般来说,如果你想从 native 端显示图像,那么最好将图像放入带有@2x 和@3x 后缀的 Resources 文件夹(使用你的 plugin.xml 中的“resource-file”属性),所以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"];
关于ios - Cordova iOS访问WWW文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37000464/