分享您的設備屏幕的問題表明,我正在尋找一種方式來共享使用UIActivityViewController當前設備屏幕。這是我的代碼到目前爲止。如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)
}
}
上面的代碼從開始讓objectsToShare行產生以下錯誤:
'_' is not convertible to 'UIImage?'
我相信這是因爲imageToShare正在返回nil。
在此先感謝。
編輯:在上面的例子中,圖像變量返回下面的值,但imageToShare返回零,所以我想問題是在那一行。
<UIImage: 0x7ffc85c12f80>, {320, 504}
只要'+ named:'只返回一個_optional_'UIImage',你需要在你想使用它之前首先解包,也許......(source:https://developer.apple.com /library/ios/documentation/UIKit/Reference/UIImage_Class/index.html#//apple_ref/occ/clm/UIImage/imageNamed :) – holex 2015-03-02 14:44:17