【问题标题】: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。我就是不知道为什么。

【问题讨论】:

    标签: ios swift swift3 uiactivityviewcontroller


    【解决方案1】:

    我想通了,swift 3 希望你更具体地初始化 UIGraphicsGetImageFromCurrentImageContext 为 UIImage 类型:

    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    

    Swift 4 中的或:

    let image = UIGraphicsGetImageFromCurrentImageContext() as Any
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      • 2013-11-16
      相关资源
      最近更新 更多