2017-08-16 64 views
1

下面我试图编写代码,这样screenShot函数将对我的viewController进行完整的截图。我试图弄清楚如何将截图放入sharePressed操作中的activityItem中,以便在尝试共享时显示屏幕截图。如何在共享按钮中获取并显示截图?

func captureScreen() -> UIImage? { 
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale) 
    view.layer.render(in: UIGraphicsGetCurrentContext()!) 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image 
} 


@IBAction func sharePressed(_ sender: Any) { 

    let activityVC = UIActivityViewController(activityItems: [""], applicationActivities: nil) 
    activityVC.popoverPresentationController?.sourceView = self.view 

    self.present(activityVC, animated: true, completion: nil) 
} 
+2

看看这个链接 https://stackoverflow.com/questions/30876068/what-is-in-swift-telling-me/30876177 –

回答

1
@IBAction func sharePressed(_ sender: Any) { 

    let imgScreenshot = captureScreen() 

    if let imgScreenshot = imgScreenshot { 
     let objectsToShare = ["Post message", imgScreenshot] as [Any] 
     let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) 
     activityVC.excludedActivityTypes = [UIActivityType.airDrop, UIActivityType.addToReadingList] 
     self.present(activityVC, animated: true, completion: nil) 
    } 
} 
相关问题