使用UIActivityViewController提供缩略图 [英] Give thumbnail image with UIActivityViewController

查看:244
本文介绍了使用UIActivityViewController提供缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过UIActivityViewController与文字共享图像.如果我这样做:

I'm trying to share an image with text through UIActivityViewController. If I do this:

        let activityVC = UIActivityViewController(activityItems: [text, image], applicationActivities: nil)
        self.presentViewController(activityVC, animated: true, completion: nil)

一切正常.问题是我只想与某些活动类型共享图像.即,当用户分享到Facebook时,我不想拥有图片,但我会做其他事情.我的问题是,从未调用过这种愚蠢的方法:

Everything works fine. The problem is that I only want to share the image with certain activity types. i.e. when a user shares to Facebook I don't want to have an image, for everything else I do though. My problem is this stupid method is never called:

optional func activityViewController(_ activityViewController: UIActivityViewController,
       thumbnailImageForActivityType activityType: String?,
                       suggestedSize size: CGSize) -> UIImage?

这应该是因为它是在UIActivityItemSource协议中定义的.有什么解决办法吗?

Which should be becuase it's defined in UIActivityItemSource protocol. Is there any work around to this?

因此,我相信在这里取得了一些进展.如果您在实例化UIActivityViewController时传递了多个self值,则可以打开itemForActivityType委托方法中的多个值.因此,如果我这样做:

So I believe to have made some headway here. Turns our if you pass multiple values of self when instantiating UIActivityViewController you can return multiple values in the itemForActivityType delegate method. So if I do this:

let activityVC = UIActivityViewController(activityItems: [self, self], applicationActivities: nil)

我可以这样返回不同的值:

I can return different values like this:

func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
    if activityType == UIActivityTypePostToFacebook {
        return ["hello", "world"]
    }
    else {
        return ["goodnight", "moon"]
    }
}

但是,似乎您只能返回两个相同类型的值.

However, it seems that you can only return two values of the same type.

我的新问题是,我该如何同时返回图像和文本?狩猎仍在继续...

My new question is now, how would I return both an image and text?? The hunt continues...

推荐答案

要共享两个不同的内容集,您必须创建两个不同的itemsource

In order to share two different set of content you have to create two different itemsource

  1. 我们可以为不同的活动类型设置不同的文本内容.将 MyStringItemSource类添加到您的视图控制器

  1. we can set different text content for different activity type.Add the MyStringItemSource class to your 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以外的所有活动类型 ,然后在VC中添加 MyImageItemSource类.

  • Our requirement is to add image to all activity type except FB,to do that add the MyImageItemSource class in your 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,在这里开始

  • Now we are ready to set UIActivityViewController,here we go

    @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共享对话: 包含图片和给定的文字

    TWITTER Shared Dialogue: Contains image and given text

    FB分享对话: 仅包含给定的文字

    这篇关于使用UIActivityViewController提供缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆