【问题标题】:Cordova iOS access WWW filesCordova iOS 访问 WWW 文件
【发布时间】:2016-08-28 06:46:14
【问题描述】:

我对 iOS 开发和 cordova 还很陌生,在如何使用 NSUrl 或 NSBundle 访问文件方面,我对 Xcode 和 Objective-C 有点困惑。我想要实现的是从我的app/Classes/MainViewController.m 访问www/images 内部的所有.png 文件,并以编程方式为每个文件创建一个新的图像视图。

我的文件结构是这样的

App Folder > 
           > .xcodeproj file
           > xcode app folder > Classes > MainViewController.m
           > www > images

我可以处理图像视图的实例化,但我对如何获取图像感到困惑。在这种情况下使用 NSBundle 是否合适?如何使用 NSURL 从 MainViewController 导航到图像位置?

谢谢。

【问题讨论】:

    标签: ios cordova url nsurl nsbundle


    【解决方案1】:

    您可以获取应用程序根目录,然后将路径添加到您的图像文件:

    NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
    NSString *imagePath = [appFolderPath stringByAppendingString:@"/www/images/some_image.png"];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    

    但一般来说,如果您尝试从本机端显示图像,那么最好将图像放入带有 @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"];
    

    【讨论】:

    • 我喜欢他关于插件开发的第二条建议。这假设“icons/”与plugin.xml 位于同一目录中。 Android 同样有自己的格式,可以根据设备的分辨率使用多个图像副本,因此最好不要使用与平台无关的 www 文件夹。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多