UIDocumentInreactionController 不允许 NSURL for .URL (Swift)

UIDocumentInreactionController won't allow NSURL for .URL (Swift)

我基本上是截取视图,将其保存到我的相机胶卷,获取 NSURL 路径并导出到 Whatsapp

    //Create the UIImage
    UIGraphicsBeginImageContext(view.frame.size)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    //Save it to camera roll round 2
    var library = ALAssetsLibrary()

    library.writeImageToSavedPhotosAlbum(image.CGImage, metadata: nil, completionBlock: {
        (path:NSURL!, error:NSError!) -> Void in

        if NSThread.currentThread() == NSThread.mainThread(){
            println("\(path)")
            var controller = UIDocumentInteractionController()
            controller.delegate = self
            controller.UTI = "net.whatsapp.image"
            controller.URL = path
            controller.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)

        }
    })

我收到这个错误:

Assertion failure in -[UIDocumentInteractionController setURL:], /SourceCache/UIKit/UIKit-3318.16.21/UIDocumentInteractionController.m:1024

但是根据 Apple 的文档,它必须是 NSURL (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionController_class/#//apple_ref/occ/instp/UIDocumentInteractionController/URL)

有什么想法吗?

一旦图像保存在相机胶卷或应用程序之外的某个位置,您就可以使用 URL 和 'assets-library scheme' 访问它。

但是UIDocumentInteractionController的URL属性只支持URL和'file scheme'。

因此您需要将图像保存到应用程序的临时目录并将 'path' 属性 设置为其文件方案 URL。