我希望我的代码在单击名为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)
    }

结果如下:

ios - 用于共享屏幕截图的事件 Controller 显示为空-LMLPHP

当我用文本字符串替换图像时,我的代码有效
    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。我只是不知道为什么。

最佳答案

我发现 swift 3 希望您更具体地将UIGraphicsGetImageFromCurrentImageContext初始化为UIImage类型:

let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!

或在Swift 4中:
let image = UIGraphicsGetImageFromCurrentImageContext() as Any

关于ios - 用于共享屏幕截图的事件 Controller 显示为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39744780/

10-15 14:12