共享屏幕截图的活动控制器显示为空
Posted
技术标签:
【中文标题】共享屏幕截图的活动控制器显示为空【英文标题】:Activity controller for sharing screenshots displays empty 【发布时间】:2016-09-28 10:24:22 【问题描述】:我希望我的代码在单击名为共享的按钮时共享屏幕截图。
按钮功能如下:
斯威夫特 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。我就是不知道为什么。
【问题讨论】:
【参考方案1】:我想通了,swift 3 希望你更具体地初始化 UIGraphicsGetImageFromCurrentImageContext 为 UIImage 类型:
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
Swift 4 中的或:
let image = UIGraphicsGetImageFromCurrentImageContext() as Any
【讨论】:
以上是关于共享屏幕截图的活动控制器显示为空的主要内容,如果未能解决你的问题,请参考以下文章