使用 UIActivityViewController 提供缩略图
Give thumbnail image with UIActivityViewController
我正在尝试通过 UIActivityViewController
与文本共享图片。如果我这样做:
let activityVC = UIActivityViewController(activityItems: [text, image], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
一切正常。问题是我只想与某些 activity 类型共享图像。即,当用户分享到 Facebook 时,我不想拥有图像,尽管我所做的一切都是如此。我的问题是这个愚蠢的方法从未被调用过:
optional func activityViewController(_ activityViewController: UIActivityViewController,
thumbnailImageForActivityType activityType: String?,
suggestedSize size: CGSize) -> UIImage?
这应该是因为它在 UIActivityItemSource
协议中定义。有什么解决办法吗?
所以我相信在这里取得了一些进展。如果在实例化 UIActivityViewController
时传递 self
的多个值,则可以在 itemForActivityType
委托方法中 return 多个值。所以如果我这样做:
let activityVC = UIActivityViewController(activityItems: [self, self], applicationActivities: nil)
我可以像这样 return 不同的值:
func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
if activityType == UIActivityTypePostToFacebook {
return ["hello", "world"]
}
else {
return ["goodnight", "moon"]
}
}
不过,您似乎只能return两个相同类型的值。
我现在的新问题是,我如何 return 同时显示图片和文字??狩猎继续...
为了共享两组不同的内容,您必须创建两个不同的项目源
我们可以为不同的 activity type.Add 设置不同的文本内容 MyStringItemSource class 你的 viewcontroller
SourceOne:
class MyStringItemSource: NSObject, UIActivityItemSource {
@objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
@objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
//You can pass different text for for diffrent activity type
if activityType == UIActivityTypePostToFacebook {
return "String for facebook"
}else{
return "String for Other"
}
}
}
我们的要求是将图像添加到除 FB 之外的所有 activity 类型 ,为此添加 MyImageItemSource class 在你的 VC.
SourceTwo:
class MyImageItemSource: NSObject, UIActivityItemSource {
@objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
@objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
//This one allows us to share image ecxept UIActivityTypePostToFacebook
if activityType == UIActivityTypePostToFacebook {
return nil
}
let Image: UIImage = UIImage(data: NSData(contentsOfURL: NSURL(string: "https://pbs.twimg.com/profile_images/604644048/sign051.gif")!)!)!
return Image
}
}
现在我们已经准备好设置 UIActivityViewController,我们开始吧
@IBAction func Test(sender: AnyObject) {
let activityVC = UIActivityViewController(activityItems: [MyStringItemSource(),MyImageItemSource()] as [AnyObject], applicationActivities: nil)
//Instead of using rootviewcontroller go with your own way.
if let window = (UIApplication.sharedApplication().delegate as? AppDelegate)?.window
{
window.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
}
}
TWITTER 分享对话:
包含图像和给定的文本
FB分享对话:
仅包含给定的文本
我正在尝试通过 UIActivityViewController
与文本共享图片。如果我这样做:
let activityVC = UIActivityViewController(activityItems: [text, image], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
一切正常。问题是我只想与某些 activity 类型共享图像。即,当用户分享到 Facebook 时,我不想拥有图像,尽管我所做的一切都是如此。我的问题是这个愚蠢的方法从未被调用过:
optional func activityViewController(_ activityViewController: UIActivityViewController,
thumbnailImageForActivityType activityType: String?,
suggestedSize size: CGSize) -> UIImage?
这应该是因为它在 UIActivityItemSource
协议中定义。有什么解决办法吗?
所以我相信在这里取得了一些进展。如果在实例化 UIActivityViewController
时传递 self
的多个值,则可以在 itemForActivityType
委托方法中 return 多个值。所以如果我这样做:
let activityVC = UIActivityViewController(activityItems: [self, self], applicationActivities: nil)
我可以像这样 return 不同的值:
func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
if activityType == UIActivityTypePostToFacebook {
return ["hello", "world"]
}
else {
return ["goodnight", "moon"]
}
}
不过,您似乎只能return两个相同类型的值。
我现在的新问题是,我如何 return 同时显示图片和文字??狩猎继续...
为了共享两组不同的内容,您必须创建两个不同的项目源
我们可以为不同的 activity type.Add 设置不同的文本内容 MyStringItemSource class 你的 viewcontroller
SourceOne:
class MyStringItemSource: NSObject, UIActivityItemSource { @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject { return "" } @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? { //You can pass different text for for diffrent activity type if activityType == UIActivityTypePostToFacebook { return "String for facebook" }else{ return "String for Other" } } }
我们的要求是将图像添加到除 FB 之外的所有 activity 类型 ,为此添加 MyImageItemSource class 在你的 VC.
SourceTwo:
class MyImageItemSource: NSObject, UIActivityItemSource { @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject { return "" } @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? { //This one allows us to share image ecxept UIActivityTypePostToFacebook if activityType == UIActivityTypePostToFacebook { return nil } let Image: UIImage = UIImage(data: NSData(contentsOfURL: NSURL(string: "https://pbs.twimg.com/profile_images/604644048/sign051.gif")!)!)! return Image } }
现在我们已经准备好设置 UIActivityViewController,我们开始吧
@IBAction func Test(sender: AnyObject) { let activityVC = UIActivityViewController(activityItems: [MyStringItemSource(),MyImageItemSource()] as [AnyObject], applicationActivities: nil) //Instead of using rootviewcontroller go with your own way. if let window = (UIApplication.sharedApplication().delegate as? AppDelegate)?.window { window.rootViewController?.presentViewController(activityVC, animated: true, completion: nil) } }
TWITTER 分享对话: 包含图像和给定的文本
FB分享对话: 仅包含给定的文本