UIActivityViewController不共享图像

时间:2017-07-19 18:27:26

标签: ios swift optimization uiimage uiactivityviewcontroller

以下代码旨在根据所选的不同平台分享不同的文字和不同的图片(UIImageNSData)。麻烦的是没有图像从activityImageShare()传递到UIActivityViewController

问题:

  

这到底有什么不妥?

     

如何更正以下代码以确保activityImageShare()   共享图像,并确保它们被选为activityType   按UIActivityViewController

代码:

1 - 四张图片,三张UIImage和一张NSData

var myImage1: UIImage! 
var myImage2: UIImage!
var myImage3: NSData!
var myImage4: UIImage!

2 - 根据所选平台定义图像以传递给UIActivityViewController

class activityImageShare: NSObject, UIActivityItemSource {
    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
        return ""
    }
    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
        switch activityType {
        case UIActivityTypePostToFacebook:
            return myImage1
        case UIActivityTypePostToTwitter:
            return myImage2
        case UIActivityTypePostToWeibo:
            return myImage3
        default:
            return myImage4
        }
    }
}

3 - 根据所选平台定义文本以传递给UIActivityViewController

class activityTextShare: NSObject, UIActivityItemSource {
    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
        return ""
    }
    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
        switch activityType {
        case UIActivityTypePostToFacebook:
            return "This is my text 1."
        case UIActivityTypePostToTwitter:
            return "This is my text 2."
        case UIActivityTypePostToWeibo:
            return "This is my text 3."
        default:
            return "This is my text 4."
        }
    }
}

4 - 根据UIActivityViewControlleractivityTextShare()中定义的文字和图片展示activityImageShare()

func myShareActivity() {
    let activity = UIActivityViewController(activityItems: [activityTextShare(), activityImageShare()], applicationActivities: nil)
    self.presentViewController(activity, animated: true, completion: nil)
}

1 个答案:

答案 0 :(得分:0)

尝试在activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController)而不是""

中返回占位符图片