我希望我的代码在点击名为share的按钮时共享屏幕截图。
按钮功能如下所示:
Swift 3
func shareBtnAction(_ sender: AnyObject) {
// first I take a screenshot of the drawing
UIGraphicsBeginImageContext(drawingAreaView.bounds.size)
drawingAreaView.image?.draw(in: CGRect(x: 0, y: 0, width: drawingAreaView.frame.size.width, height: drawingAreaView.frame.size.height))
// defining the variable for activityItems
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// Initialize and present UIActivityViewController
let activity = UIActivityViewController(activityItems: [image], applicationActivities: nil)
present(activity, animated: true, completion: nil)
}
结果看起来像这样:
当我用文本字符串
替换图像时,我的代码有效 func shareBtnAction(_ sender: AnyObject) {
// defining the variable for activityItems
let text = "This is a test"
// Initialize and present UIActivityViewController
let activity = UIActivityViewController(activityItems: [text], applicationActivities: nil)
present(activity, animated: true, completion: nil)
}
所以我猜我的代码不能识别图像是UIImage。我只是无法弄清楚原因。
答案 0 :(得分:3)
我想, swift 3 希望您更具体地将UIGraphicsGetImageFromCurrentImageContext初始化为UIImage类型:
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
或者在Swift 4中:
let image = UIGraphicsGetImageFromCurrentImageContext() as Any