【问题标题】:How to access a pdf file from iOS Titanium module which is under Titanium mobile project resource folder?如何从 Titanium 移动项目资源文件夹下的 iOS Titanium 模块访问 pdf 文件?
【发布时间】:2013-03-06 13:34:55
【问题描述】:

我正在创建一个钛移动应用程序,其中包含用于PDF 编辑的 iOS 模块。我在钛项目(Titanium Studio)资源目录中有一个 pdf 文件。如何从 iOS 模块访问 PDF 文件?

我使用Titanium.Filesystem.getResourcesDirectory()+'filename.pdf';获得了文件路径

并将路径作为方法参数传递给 iOS 模块。

如何获取iOS Module中的文件??

【问题讨论】:

    标签: ios path titanium


    【解决方案1】:

    你可以使用 TiUtils 的方法“toUrl:proxy:”。文档中的示例如下,但它也适用于您从 JS 领域获取的 arg(只需将“路径”字符串替换为您自己的):

    NSString *path = [NSString stringWithFormat:@"modules/%@/foo.png",[self moduleId]];
    NSURL *url = [TiUtils toURL:path proxy:self];
    UIImage *image = [TiUtils image:url proxy:self];
    

    "self" 可以是您的模块,因为 TiModules 本身就是代理(换句话说,将其保留为 self 对您来说可能没问题)。

    想要更完整的示例?看看我前段时间写的 AirPrint 模块。 "print:(id)args" 方法接受一个 { url: "whatever.pdf" },将其转换为一个 URL,并在 UI 线程上用它做一些有趣的事情。

    - (void)print:(id)args
    {
        ENSURE_UI_THREAD(print,args);
        ENSURE_SINGLE_ARG(args,NSDictionary);
    
        NSURL* url = [TiUtils toURL:[args objectForKey:@"url"] proxy:self];
        if (url==nil) {
            NSLog(@"[ERROR] Print called without passing in a url property!");
            return;
        }
    

    【讨论】:

    • +1 不知道 [TiUtils toURL:arg] 或 TiModule selfing 。非常好!
    【解决方案2】:

    如果它在 Resources 目录中,那么只需将名称传递给模块,因为这意味着 PDF 位于主应用程序资产包中,请使用此代码获取它的路径:

    // The application assets can be accessed by building a path from the mainBundle of the application.
    NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"filename.pdf"];
    

    Heres a reference to this inside the moddevguide for iOS from Titanium.

    作为附录,如果您发现自己试图将完整路径传递给 iOS 中的模块,您需要这样做resolve the native path

    var file = Titanium.Filesystem.getResourcesDirectory()+'filename.pdf';
    var actualPathToPassToModule = file.resolve();
    

    【讨论】:

    • 我认为您回答了处理文件的所有方式,除了他要求的方式。 :) 不过还是有用的信息。
    • 是的,我错过了那个。也许它会帮助某人。我会编辑,但你有一个很好的答案!
    • 但是要获取 file.resolve() 我们需要使用 -> var file = Ti.Filesystem.getFile(Titanium.Filesystem.getResourcesDirectory() + 'document.pdf');
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    相关资源
    最近更新 更多