我有以下代码,用于从
Swift应用程序在instagram上共享图像:
@IBAction func instagramShareButton(sender:AnyObject){
@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) 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在那里,当我点击它,我得到以下错误:
Assertion failure in -[_UIOpenWithAppActivity performActivity],
/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3505.16/UIDocumentInteractionController.m:408*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’,reason:
‘UIDocumentInteractionController has gone away prematurely!’
我相信我不得不释放UIDocumentInteractionController对象.我对吗?
没有找到任何信息来帮助我了解如何在Swift中做到这一点.请帮我找出如何解决这个问题.
我想你是对的
我有同样的问题.
原文链接:https://www.f2er.com/swift/319661.html我有同样的问题.
在开始共享功能之前,必须从UIDocumentInteractionController创建一个全局变量:
var interactionController: UIDocumentInteractionController? @IBAction func instagramShareButton(sender: AnyObject) { ... interactionController = UIDocumentInteractionController(URL: fileURL) interactionController!.UTI = "com.instagram.exclusivegram" let msgBody = "My message" interactionController!.annotation = NSDictionary(object: msgBody,forKey: "InstagramCaption") interactionController!.presentOpenInMenuFromRect(rect,animated: true) }
这对我有用