共享图片和文字给Facebook Messenger的使用UIActivityViewController失败

问题描述:

Question共享图片和文字给Facebook Messenger的使用UIActivityViewController失败

What changes to the code below must be made to ensure Messenger plays nicely with UIActivityViewController and shares both the image and text, or at the very least, the image?


背景

我使用UIActivityViewController从我的应用程序共享文本和图像,并将它们发送到电子邮件,消息和其他共享应用程序。 UIActivityViewController是伟大的,并与大多数应用程序以简单和标准的方式工作......但是,我与Messenger(Facebook Messenger)不想合作的问题。

在下面的代码,我一个轻按UIButton这需要在屏幕的快照图像,把它变成一个.png和然后用我的准备文本(textShare)到UIActivityViewController一起发送准备的图像(imageShare)。这个简单的方法允许我的应用程序成功地分享电子邮件,消息和除Messenger以外的许多其他共享应用

(侧面说明,Facebook是唯一能够分享准备好的图像(imageShare),而不是文本。)


问题

尝试与UIActivityViewController分享时,这些都是问题给Messenger:

  • 当共享activityItems: [textShare, imageShare]

    • Messenger只发送共享文本。
  • 当共享activityItems: [textShare]

    • 选项分享到Messenger甚至没有在UIActivityViewController可用。
  • 当共享activityItems: [imageShare]

    • ,则显示错误:“无法加载内容。加载您的内容时出现问题。请重试。”

代码

@IBAction func myButton(sender: UIButton) { 

    // Take snapshot of screen 
    let imageSnapshot: UIImage! 
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0) 
    self.view.drawViewHierarchyInRect(CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height), afterScreenUpdates: false) 
    imageSnapshot = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    // Prepare text to share 
    let textShare: String! 
    textShare = "This is my original text." 

    // Prepare image to share 
    let imageShare: NSData 
    imageShare = UIImagePNGRepresentation(imageSnapshot)! 

    // Share text and image 
    let activity = UIActivityViewController(activityItems: [textShare, imageShare], applicationActivities: nil) 
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone { 
     self.presentViewController(activity, animated: true, completion: nil) 
    } 

} 

图片

的iOS呈现UIActivityViewController与可用的应用程序。

UIActivityViewController screenshot

尝试使用UIImage的NSData的代替。你可以将UIImage转换为AnyObject并添加到你的数组中。

而且看这个问题:Sharing image using UIActivityViewController

+0

感谢您的建议。当我将'NSData'改为'UIImage'时,我得到一个错误:'不能指定'NSData'类型的值来键入'UIImage'。你能扩展你的答案,显示在上下文中的代码更改吗? – user4806509

+0

他并不是说要用'UIImage'替换类型(在imageShare')'NSData'旁边,而是从'UIImage'中发送'UIImage'实例,而不是从'UIImage'中创建'NSData'实例。 – shim

let textShare = "This is my original text." 
var shareObject = [AnyObject]() 
if textShare != nil { 
    shareObject.append(textShare as AnyObject) 
} 

let Yourimage = UIImage(named: "image.png") 
let imageData = UIImagePNGRepresentation(Yourimage!) as NSData? 

if let data = imageData { 
    shareObject.append(data) 
} 

if shareObject.count > 0 { // check condition for text and image 
    let activityViewController = UIActivityViewController(activityItems: shareObject, applicationActivities: nil) 
    activityViewController.popoverPresentationController?.sourceView = self.view 
    present(activityViewController, animated: true, completion: nil) 
} 
+0

谢谢你的回答,但这不能解决问题,图像仍然丢失,只显示文本。 – user4806509

所以需要的是什么到底是转换NSDataUIImage的一个额外的步骤。

Note, the one issue with this approach of converting NSData back into UIImage is that it produces image quality that is either lossless (i.e. Message and Mail) or lossy (i.e. Notes, Photos, Messenger).

Interestingly Messages, Mail, and a variety of third party sharing apps were quite content sharing the NSData in the UIActivityViewController whereas Messenger would just not tolerate it. (I'd be interested to understand why exactly?)


变化

从这:

// Prepare image to share 
    let imageShare: NSData 
    imageShare = UIImagePNGRepresentation(imageSnapshot)! 

要的是:

// Prepare image to share 
    let imageShareData: NSData 
    imageShareData = UIImagePNGRepresentation(imageSnapshot)! 
    let imageShare = UIImage(data: imageShareData)! 

最后的工作代码

@IBAction func myButton(sender: UIButton) { 

    // Take snapshot of screen 
    var imageSnapshot: UIImage! 
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0) 
    self.view.drawViewHierarchyInRect(CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height), afterScreenUpdates: false) 
    imageSnapshot = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    // Prepare text to share 
    let textShare: String! 
    textShare = "This is my original text." 

    // Prepare image to share 
    let imageShareData: NSData 
    imageShareData = UIImagePNGRepresentation(imageSnapshot)! 
    let imageShare = UIImage(data: imageShareData)! 

    // Share text and image 
    let activity = UIActivityViewController(activityItems: [textShare, imageShare], applicationActivities: nil) 
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone { 
     self.presentViewController(activity, animated: true, completion: nil) 
    } 

} 
+0

你有一个'UIImage',你将它转换为'NSData',然后你将它转换回'UIImage'而不做任何事情。只需消除不必要的步骤并发送原始的'UIImage'实例即可。 – shim

+0

谢谢@shim。问题是原始UIImage保存时似乎是一个有损JPG。我想保留一个清晰的截图,这就是为什么我转换为PNG。 – user4806509

+0

那你为什么问[这个问题](https://*.com/q/45153494/1032372)? – shim