以下代码旨在根据所选的不同平台分享不同的文字和不同的图片(UIImage
和NSData
)。麻烦的是没有图像从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 - 根据UIActivityViewController
和activityTextShare()
中定义的文字和图片展示activityImageShare()
。
func myShareActivity() {
let activity = UIActivityViewController(activityItems: [activityTextShare(), activityImageShare()], applicationActivities: nil)
self.presentViewController(activity, animated: true, completion: nil)
}
答案 0 :(得分:0)
尝试在activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController)
而不是""