gpt4 book ai didi

ios - 使用类似于 SLComposeViewController 的 Swift 将 UIImage 发布到 Instagram

转载 作者:可可西里 更新时间:2023-11-01 04:09:37 24 4
gpt4 key购买 nike

我有一个正在处理的 iOS Xcode 7 Swift 2 项目。该应用使用以下方式将照片发布到 FacebookTwitter:

var shareToFacebook: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)

var shareToTwitter: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)

喜欢将照片发布到这两个社交媒体是多么容易和简单。我不需要也不想要它们的 API。

我想为 Instagram 做一些类似和简单的事情。无需处理 Instagram API 即可执行此操作的最佳方法是什么?还是我别无选择?

我的照片 data 是使用 NSCoding 保存的,而不是在 DocumentDirectory 中。我看了here用于集成,但这是用于保存在应用 目录 中的照片。

就像我已经拥有的 Facebook 和 Twitter 一样简单。

最佳答案

针对 Swift 3 进行了更新

import UIKit
import Foundation

class InstagramManager: NSObject, UIDocumentInteractionControllerDelegate {

private let kInstagramURL = "instagram://app"
private let kUTI = "com.instagram.exclusivegram"
private let kfileNameExtension = "instagram.igo"
private let kAlertViewTitle = "Error"
private let kAlertViewMessage = "Please install the Instagram application"

var documentInteractionController = UIDocumentInteractionController()

// singleton manager
class var sharedManager: InstagramManager {
struct Singleton {
static let instance = InstagramManager()
}
return Singleton.instance
}

func postImageToInstagramWithCaption(imageInstagram: UIImage, instagramCaption: String, view: UIView) {
// called to post image with caption to the instagram application

let instagramURL = NSURL(string: kInstagramURL)

if UIApplication.shared.canOpenURL(instagramURL! as URL) {

let jpgPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(kfileNameExtension)

do {
try UIImageJPEGRepresentation(imageInstagram, 1.0)?.write(to: URL(fileURLWithPath: jpgPath), options: .atomic)

} catch {

print(error)
}

let rect = CGRect(x: 0, y: 0, width: 612, height: 612)
let fileURL = NSURL.fileURL(withPath: jpgPath)
documentInteractionController.url = fileURL
documentInteractionController.delegate = self
documentInteractionController.uti = kUTI

// adding caption for the image
documentInteractionController.annotation = ["InstagramCaption": instagramCaption]
documentInteractionController.presentOpenInMenu(from: rect, in: view, animated: true)
} else {

/// display some message about how it didn't work
/// Like: UIAlertController(title: kAlertViewTitle, message: kAlertViewMessage, preferredStyle: .alert)
}
}
}

然后在您要调用它的 ViewController 中...

InstagramManager.sharedManager.postImageToInstagramWithCaption(imageInstagram: <YOUR IMAGE>, instagramCaption: shareText, view: self.view)

如果安装了 Instagram,这将打开“打开方式”功能

关于ios - 使用类似于 SLComposeViewController 的 Swift 将 UIImage 发布到 Instagram,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36406515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com