gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 16:05:08 26 4
gpt4 key购买 nike

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

您可以通过传递您喜欢的任何 View 来截取屏幕截图:

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

关于ios - 我不知道如何在 Swift 中将屏幕截图发送到 Instagram 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40400770/

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