【发布时间】:2015-03-02 14:21:55
【问题描述】:
正如问题所暗示的,我正在寻找一种在使用 UIActivityViewController 时共享当前设备屏幕的方法。到目前为止,这是我的代码。
@IBAction func buttonShareTapped(sender: UIButton) {
let textToShare = "Here's my text to be shared!"
// Generate the screenshot
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
var image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
var imageToShare = UIImage(named: "\(image)")
if let myWebsite = NSURL(string: "http://mywebsite.com/")
{
let objectsToShare = [textToShare, imageToShare, myWebsite]
let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
// Excluded Activities Code
activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]
self.presentViewController(activityViewController, animated: true, completion: nil)
}
}
上面的代码从 Let objectsToShare 开始的行产生以下错误:
'_' is not convertible to 'UIImage?'
我认为这是因为 imageToShare 目前返回 nil。
提前致谢。
edit:在上面的示例中,图像变量返回下面的值,但 imageToShare 返回 nil,所以我猜问题出在那一行。
<UIImage: 0x7ffc85c12f80>, {320, 504}
【问题讨论】:
-
只要
+named:只返回一个可选UIImage,你需要先打开包装才能使用它,也许......(来源:developer.apple.com/library/ios/documentation/UIKit/Reference/…)
标签: swift uiactivityviewcontroller