截图并使用分享按钮分享
Taking Screenshot and share with share button
我是初学者,我正在尝试截取该应用程序的屏幕截图并使用分享按钮进行分享。分享按钮有效,它让我分享了我的初始文本和 URL,只是截图似乎甚至没有被截取。
经过几天的研究,我发现所有的答案对我来说都太高级了,我可能一开始就缺少基础知识。
我不确定将获取屏幕截图的代码放在哪里,或者我必须设置什么才能使其正常工作。
我也不知道怎么把截图实现成图片
这是我的分享按钮代码,效果很好:
func socialShare(sharingText: String?, sharingImage: UIImage?, sharingURL: NSURL?) {
var sharingItems = [AnyObject]()
if let text = sharingText {
sharingItems.append(text)
}
if let image = sharingImage {
sharingItems.append(image)
}
if let url = sharingURL {
sharingItems.append(url)
}
let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop,UIActivityTypeAddToReadingList,
UIActivityTypeAssignToContact,UIActivityTypePostToTencentWeibo,UIActivityTypePostToVimeo,UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,UIActivityTypePostToWeibo]
self.presentViewController(activityViewController, animated: true, completion: nil)
}
@IBAction func clickShare(sender: AnyObject) {
socialShare("Text to share #Hashtag", sharingImage: UIImage(named: "image"), sharingURL: NSURL(string: "http://itunes.apple.com/app/"))
}
这是我到处找到的截屏代码。但是我没有让它工作/不知道把它放在哪里,如何将它与 IBAction 连接以及如何将屏幕截图实现为图像。
//Generate the screenshot
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
var image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
我非常感谢能指导我了解缺失的基础知识的答案。非常感谢!
快速截图(无运营商状态栏)和分享代码示例:
let bounds = UIScreen.mainScreen().bounds
UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
self.view.drawViewHierarchyInRect(bounds, afterScreenUpdates: false)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let activityViewController = UIActivityViewController(activityItems: [img], applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
分享在Swift 3.1
@IBAction func shareButtonClicked(_ sender: Any) {
//Set the default sharing message.
let message = "Hello!"
let link = NSURL(string: "http://whosebug.com/")
// Screenshot:
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, true, 0.0)
self.view.drawHierarchy(in: self.view.frame, afterScreenUpdates: false)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//Set the link, message, image to share.
if let link = link, let img = img {
let objectsToShare = [message,link,img] as [Any]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityType.airDrop, UIActivityType.addToReadingList]
self.present(activityVC, animated: true, completion: nil)
}
}
Swift 4 个作品。
let bounds = UIScreen.main.bounds
UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
self.view.drawHierarchy(in: bounds, afterScreenUpdates: false)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let activityViewController = UIActivityViewController(activityItems: [img!], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
我是初学者,我正在尝试截取该应用程序的屏幕截图并使用分享按钮进行分享。分享按钮有效,它让我分享了我的初始文本和 URL,只是截图似乎甚至没有被截取。
经过几天的研究,我发现所有的答案对我来说都太高级了,我可能一开始就缺少基础知识。
我不确定将获取屏幕截图的代码放在哪里,或者我必须设置什么才能使其正常工作。 我也不知道怎么把截图实现成图片
这是我的分享按钮代码,效果很好:
func socialShare(sharingText: String?, sharingImage: UIImage?, sharingURL: NSURL?) {
var sharingItems = [AnyObject]()
if let text = sharingText {
sharingItems.append(text)
}
if let image = sharingImage {
sharingItems.append(image)
}
if let url = sharingURL {
sharingItems.append(url)
}
let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop,UIActivityTypeAddToReadingList,
UIActivityTypeAssignToContact,UIActivityTypePostToTencentWeibo,UIActivityTypePostToVimeo,UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,UIActivityTypePostToWeibo]
self.presentViewController(activityViewController, animated: true, completion: nil)
}
@IBAction func clickShare(sender: AnyObject) {
socialShare("Text to share #Hashtag", sharingImage: UIImage(named: "image"), sharingURL: NSURL(string: "http://itunes.apple.com/app/"))
}
这是我到处找到的截屏代码。但是我没有让它工作/不知道把它放在哪里,如何将它与 IBAction 连接以及如何将屏幕截图实现为图像。
//Generate the screenshot
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
var image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
我非常感谢能指导我了解缺失的基础知识的答案。非常感谢!
快速截图(无运营商状态栏)和分享代码示例:
let bounds = UIScreen.mainScreen().bounds
UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
self.view.drawViewHierarchyInRect(bounds, afterScreenUpdates: false)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let activityViewController = UIActivityViewController(activityItems: [img], applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
分享在Swift 3.1
@IBAction func shareButtonClicked(_ sender: Any) {
//Set the default sharing message.
let message = "Hello!"
let link = NSURL(string: "http://whosebug.com/")
// Screenshot:
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, true, 0.0)
self.view.drawHierarchy(in: self.view.frame, afterScreenUpdates: false)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//Set the link, message, image to share.
if let link = link, let img = img {
let objectsToShare = [message,link,img] as [Any]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityType.airDrop, UIActivityType.addToReadingList]
self.present(activityVC, animated: true, completion: nil)
}
}
Swift 4 个作品。
let bounds = UIScreen.main.bounds
UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
self.view.drawHierarchy(in: bounds, afterScreenUpdates: false)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let activityViewController = UIActivityViewController(activityItems: [img!], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)