【发布时间】:2019-06-07 05:11:53
【问题描述】:
我想通过我的 iOS 应用打开带有图像的 Instagram 应用。我的应用程序有一个图片 URL,我想用图片 URL 中包含的图片打开 Instagram 页面。
我用来打开 Instagram 应用程序的 URL 如下所示:
instagram://library?AssetPath="+imageURL
【问题讨论】:
标签: ios swift iphone instagram
我想通过我的 iOS 应用打开带有图像的 Instagram 应用。我的应用程序有一个图片 URL,我想用图片 URL 中包含的图片打开 Instagram 页面。
我用来打开 Instagram 应用程序的 URL 如下所示:
instagram://library?AssetPath="+imageURL
【问题讨论】:
标签: ios swift iphone instagram
使用以下代码在 Instagram 分享图片,希望对您有所帮助。
首先从您的图片 URL 获取图片(下载),并在此函数中用作图片(下载的图片)。
@IBAction func shareImageInstagram(_ sender: Any) {
DispatchQueue.main.async {
//Share To Instagram:
let instagramURL = URL(string: "instagram://app")
if UIApplication.shared.canOpenURL(instagramURL!) {
let imageData = UIImageJPEGRepresentation(image, 100)
let writePath = (NSTemporaryDirectory() as NSString).appendingPathComponent("instagram.igo")
do {
try imageData?.write(to: URL(fileURLWithPath: writePath), options: .atomic)
} catch {
print(error)
}
let fileURL = URL(fileURLWithPath: writePath)
self.documentController = UIDocumentInteractionController(url: fileURL)
self.documentController.delegate = self
self.documentController.uti = "com.instagram.exlusivegram"
if UIDevice.current.userInterfaceIdiom == .phone {
self.documentController.presentOpenInMenu(from: self.view.bounds, in: self.view, animated: true)
} else {
self.documentController.presentOpenInMenu(from: self.IGBarButton, animated: true)
}
} else {
print(" Instagram is not installed ")
}
}
}
【讨论】:
//希望对你有帮助
func shareImage(_ image: UIImage?, withCaption caption: String?) {
var img: UIImage? = image
let instagramURL: URL = URL(string: "instagram://")!
if UIApplication.shared.canOpenURL(instagramURL) == true {
if img != nil {
let jpgPath = NSTemporaryDirectory().stringByAppendingPathComponent("instagram.igo")
do {
try UIImageJPEGRepresentation(img!, 1.0)!.write(to: URL(string: jpgPath)!, options: .atomic)
} catch _ {
}
//writeToFile(jpgPath, atomically:true)
let rect = CGRect(x: 0, y: 0, width: 0, height: 0)
let fileURL: URL = URL(fileURLWithPath: jpgPath)
let docVc = UIDocumentInteractionController(url: fileURL)
docVc.uti = "com.instagram.exclusivegram"
if caption != nil && caption?.count ?? 0 > 0 {
docVc.annotation = NSDictionary(object: caption!, forKey: "InstagramCaption" as NSCopying)
}
docVc.presentOpenInMenu(from: rect, in: self.view, animated: true)
}
} else {
// please install instagram
}
}
【讨论】: