这次做的项目中需要分享图片到Instagram,于是看了下Instagram的官方文档,要分享图片到Instagram必须使用UIDocumentInteractionController。于是写了一个,废话少说,直接上代码
if let docPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
let imagePath = docPath.appending("/share.igo")
if let imageData = UIImagePNGRepresentation(UIImage.init(named: "map_slide_unlock_arrow_icon")!) {
let nsData: NSData = imageData as NSData
nsData.write(toFile: imagePath, atomically: true)
docController = UIDocumentInteractionController(url: URL(fileURLWithPath: imagePath))
docController?.delegate = self
docController?.uti = "com.instagram.exclusivegram"
docController?.url = NSURL.fileURL(withPath: imagePath)
docController?.presentOpenInMenu(from: vc!.view.bounds, in: vc!.view, animated: true)
}
}
效果如下图所示
Paste_Image.png值得一提的是我们处于天朝局域网,要想上Instagram必须要翻墙。另外现在是把图片分享过去了,但是不知道怎么设置Instagram默认的描述文字,如果有知道的大神请指点
网友评论