2016-11-03 4 views
0

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() 
+0

Googleで検索してみましたか? Instagram API、Instagram Kit? –

+0

はい..!しかし、目的のために働いていない - C ..!コードを迅速に変換することはできません。 – AshishVerma

+0

を参照してください :http://stackoverflow.com/questions/40303966/instagram-sharing-button-for-ios-9-10/40309427#40309427 –

答えて

0

スウィフト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] 
} 

今、あなたはこのあなたのViewControllerインポートではUIDocumentInteractionController

を使用してInstagramの上の画像を共有することができます:

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") 
    } 
} 

Teste dをXcode 8.1、iOS 10.1デバイスにインストールします。うまくいった。

関連する問題