使用UIActivityViewController将图像和文本共享到Facebook Messenger失败 [英] Sharing image and text to Facebook Messenger with UIActivityViewController failing

查看:218
本文介绍了使用UIActivityViewController将图像和文本共享到Facebook Messenger失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

必须对下面的代码进行哪些更改以确保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)不想与之合作的问题.

I am using UIActivityViewController to share text and images from my app and send them to email, messages and other sharing apps. UIActivityViewController is great and works in a simple and standard way with most apps… but, I’m having issues with Messenger (Facebook Messenger) which doesn’t want to cooperate.

在下面的代码中,我点击一个UIButton,以获取屏幕快照,将其转换为.png,然后将准备好的图像(imageShare)连同准备好的文本(textShare )到UIActivityViewController.通过这种简单的方法,我的应用程序可以成功共享到电子邮件,消息和Messenger以外的其他许多共享应用程序.

In the below code, I tap a UIButton which takes a snapshot image of the screen, turns it into a .png, and then sends the prepared image (imageShare) along with my prepared text (textShare) to the UIActivityViewController. This simple method allows my app to successfully share to email, messages and many other sharing apps except Messenger.

(注意,Facebook仅能共享准备好的图像(imageShare),而不能共享文本.)

(Side note, Facebook is only able to share the prepared image (imageShare) but not the text.)

问题

这些是尝试与UIActivityViewController共享给Messenger时出现的问题:

These are the problems when trying to share with UIActivityViewController to Messenger :

  • 共享activityItems: [textShare, imageShare]

  • When sharing activityItems: [textShare, imageShare]

  • 信使仅发送共享文本.

共享activityItems: [textShare]

When sharing activityItems: [textShare]

  • UIActivityViewController中甚至没有共享给Messenger的选项.
  • The option to share to Messenger is not even available in UIActivityViewController.

共享activityItems: [imageShare]

When sharing 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提供可用的应用程序.

iOS presenting the UIActivityViewController with available applications.

推荐答案

最后,需要做的是将NSData转换回UIImage的额外步骤.

So in the end what was needed was an extra step of converting NSData back into UIImage.

注意,这种将NSData转换回去的方法存在一个问题 进入UIImage是因为它产生的图像质量是 无损(即邮件和邮件)或无损(即便笺,照片, 信使).

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).

有趣的消息,邮件和各种第三方共享 应用程序中的内容完全共享NSData UIActivityViewController,而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)
    }

}

这篇关于使用UIActivityViewController将图像和文本共享到Facebook Messenger失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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