使用UIImageView
分享UIActivityViewController
的图片?
我正在尝试使用UIActivityViewController
分享图片,但收到错误:
func shareIMG(){
let activityItems = [self.imageView.image]
let avc = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
self.presentViewController(avc, animated: true, completion: nil)
}
错误讯息:
找不到类型为'UIActivityViewController'的初始值设定项 接受类型'的参数列表'(activityItems:[UIImage?], applicationActivities:nil)'
有什么想法吗?
答案 0 :(得分:9)
let activityItem: [AnyObject] = [self.imageView.image as! AnyObject]
let avc = UIActivityViewController(activityItems: activityItem as [AnyObject], applicationActivities: nil)
self.presentViewController(avc, animated: true, completion: nil)
注意:以上代码在Xcode 6.3模拟器中进行了测试
答案 1 :(得分:4)
只需创建一个按钮及其动作即可。然后像这样安装按钮的动作。
注意:您必须通过设置登录您的Facebook或Twitter。然后这样做。
@IBAction func onShareTouched(sender: AnyObject) {
print("share")
let myShare = "My beautiful photo! <3 <3"
let image: UIImage = UIImage(named: "yourImageNameHere")
let shareVC: UIActivityViewController = UIActivityViewController(activityItems: [(image), myShare], applicationActivities: nil)
self.presentViewController(shareVC, animated: true, completion: nil)
}
答案 2 :(得分:2)
适用于Swift的iPhone和iPad的完整代码:
func share(sender: UIButton){
let img: UIImage = UIImage(named: "lenna")!
let messageStr:String = "some sharing text"
let shareItems:Array = [img, messageStr]
activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController!.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone {
self.presentViewController(activityViewController!, animated: true, completion: nil)
} else {
let popup: UIPopoverController = UIPopoverController(contentViewController: activityViewController!)
popup.presentPopoverFromRect(sender.bounds, inView: sender, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}
}
答案 3 :(得分:0)
看起来你需要用!
打开activityItems