我不知道如何将屏幕截图发送到 Swift 中的 Instagram 应用程序



我正在尝试使用Swift实现图像共享到Instagram应用程序。

我确实知道如何截图,但我不知道如何将截图分享到Instagram应用程序。

这是我的快照代码:

_ = UIScreen.main
    if UIApplication.shared.keyWindow != nil {
        let top: CGFloat = 101
        let bottom: CGFloat = 96
        // The size of the cropped image
        let size = CGSize(width: view.frame.size.width, height: view.frame.size.height - top - bottom)
        // Start the context
        UIGraphicsBeginImageContext(size)

        // we are going to use context in a couple of places
        let context = UIGraphicsGetCurrentContext()!
        // Transform the context so that anything drawn into it is displaced "top" pixels up
        // Something drawn at coordinate (0, 0) will now be drawn at (0, -top)
        // This will result in the "top" pixels being cut off
        // The bottom pixels are cut off because the size of the of the context
        context.translateBy(x: 0, y: 0)
        // Draw the view into the context (this is the snapshot)
        UIGraphicsBeginImageContextWithOptions(size,view.isOpaque, 0)
        self.view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
        let snapshot = UIGraphicsGetImageFromCurrentImageContext()
        // End the context (this is required to not leak resources)
        UIGraphicsEndImageContext()

迅速3。x代码

你可以通过传递你喜欢的视图来截图:

func getScreenshot(viewToSnapShot:UIView) -> UIImage {
    UIGraphicsBeginImageContext(viewToSnapShot.frame.size)
    viewToSnapShot.layer.render(in: UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image!
}

并获取documentDirectory路径。我编写了一个简单的函数,它将返回文档目录路径。

func documentsDirectory() -> String {
    return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
}

现在你可以使用UIDocumentInteractionController在instagram上分享图片了

ViewController中导入:

import Social
//make UIDocumentInteractionController object
var docController:UIDocumentInteractionController!

然后当你想在instagram上发布截图/图片时调用这个函数

// On calling this function, will open your document controller
func postImageToInstagram() {
    let instagram = URL(string: "instagram://app")!
    if UIApplication.shared.canOpenURL(instagram) {
        //By Passing self.view, I am actually taking the screenshot
        let imageToBePosted = getScreenshot(viewToSnapShot: self.view)
        let imageURL = URL(fileURLWithPath: documentsDirectory())
        let fullPath = imageURL.appendingPathComponent("instagram.igo")
        do {
            try UIImagePNGRepresentation(imageToBePosted)!.write(to: fullPath)
            let rect = CGRect(x: 0, y: 0, width: 0, height: 0)
            let igImageHookFile = URL(string: "file://(fullPath)")
            docController = UIDocumentInteractionController(url: igImageHookFile!)
            docController.uti = "com.instagram.photo"
            docController.presentOpenInMenu(from: rect, in: self.view, animated: true)
        } catch {
            print(error)
        }  
    } else {
        print("Instagram not installed")
    }
}

测试在Xcode 8.1, iOS 10.1设备。工作得很好。

最新更新