用于共享屏幕截图的活动控制器显示为空

问题描述:

我希望我的代码在单击名为share的按钮时共享屏幕截图。用于共享屏幕截图的活动控制器显示为空

按钮功能如下:

斯威夫特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) 
    } 

结果,但是看起来是这样的:当我更换

enter image description here

我的代码工作带有文本字符串的图像

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。我无法弄清楚为什么。

我想通了,迅速3希望你能更具体的初始化UIGraphicsGetImageFromCurrentImageContext作为一个UIImage的类型:

let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!