【发布时间】:2015-09-23 11:51:53
【问题描述】:
我有以下代码用于从我的 Swift 应用程序在 Instagram 上共享图像: @IBAction func instagramShareButton(sender: AnyObject) {
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let path = documentsDirectory.stringByAppendingPathComponent("Share Icon.igo")
let imageName: String = "Share Icon.png"
let image = UIImage(named: imageName)
let data = UIImagePNGRepresentation(image!)
data!.writeToFile(path, atomically: true)
let imagePath = documentsDirectory.stringByAppendingPathComponent("Share Icon.igo")
let rect = CGRectMake(0, 0, 0, 0)
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0)
self.view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
UIGraphicsEndImageContext()
let fileURL = NSURL(fileURLWithPath: imagePath)
print("fileURL = \(fileURL)")
var interactionController = UIDocumentInteractionController(URL: fileURL)
interactionController.delegate = self
interactionController.UTI = "com.instagram.exclusivegram"
let msgBody = "My message"
interactionController.annotation = NSDictionary(object: msgBody, forKey: "InstagramCaption")
interactionController.presentOpenInMenuFromRect(rect, inView: self.view, animated: true)
}
func documentInteractionControllerWillPresentOpenInMenu(controller: UIDocumentInteractionController) { }
我将代码从 Objective C 翻译成 Swift,因为我没有在 Swift 中找到任何可以在 Instagram 上分享的内容。
菜单弹出,我在那里看到Instagram,当我点击它时,我收到以下错误:
-[_UIOpenWithAppActivity performActivity] 中的断言失败, /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3505.16/UIDocumentInteractionController.m:408
*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因: 'UIDocumentInteractionController 过早地消失了!'
我相信我必须以某种方式释放 UIDocumentInteractionController 对象。我对吗? 还没有找到任何信息来帮助我理解如何在 Swift 中做到这一点。请帮我弄清楚如何解决这个问题。
【问题讨论】:
-
这可能是我不久前尝试做同样事情时遇到的同样问题。我从未找到解决方案,但能够实施令我的应用程序满意的解决方法。你可以在这里看到我的帖子:link
标签: swift share instagram release retain