简体   繁体   中英

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

I am trying to implement image sharing to Instagram app using Swift.

I do know how to take screenshot but I am unable to get how to share the screenshot to Instagram application.

Here is my snapshot code:

_ = 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 code

You can take screenshot by passing the whichever view you like:

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

and grab the documentDirectory path too. I made a simple function that will return the document directory path.

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

Now you can share the image on instagram using the UIDocumentInteractionController

In your ViewController import this:

import Social

//make UIDocumentInteractionController object
var docController:UIDocumentInteractionController!

And then call this function whenever you want to post screenshot/image on 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")
    }
}

Tested on Xcode 8.1, iOS 10.1 device. Worked Fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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