繁体   English   中英

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

[英]I don't know how to send screenshot to Instagram application in Swift

我正在尝试使用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()

Swift 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设备上进行了测试。 工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM