【问题标题】:Send Image and Text With Whatsapp使用 Whatsapp 发送图像和文本
【发布时间】:2015-11-02 09:30:17
【问题描述】:

我需要从我的应用中发送带有文本的图像,我知道如何仅发送图像或仅发送文本,但我不知道如何将两者结合起来。

只是一张图片:

    let image = UIImage(named: "Image") // replace that with your UIImage

    let filename = "myimage.wai"
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, false)[0] as! NSString
    let destinationPath = documentsPath.stringByAppendingString("/" + filename).stringByExpandingTildeInPath
    UIImagePNGRepresentation(image).writeToFile(destinationPath, atomically: false)
    let fileUrl = NSURL(fileURLWithPath: destinationPath)! as NSURL

    documentController = UIDocumentInteractionController(URL: fileUrl)
    documentController.delegate = self
    documentController.UTI = "net.whatsapp.image"
    documentController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: false)    

只是一个文本:

    var whatsappURL = NSURL(string: "whatsapp://send?text=hello,%20world")

    if UIApplication.sharedApplication().canOpenURL(whatsappURL!) {
        UIApplication.sharedApplication().openURL(whatsappURL!)
    }    

如何发送带文字的图片?

编辑#1

我找到了一个代码,可以将带有文本的图像共享到whatsapp,但它是用java编写的,你能把它翻译成swift吗?

Intent whatsappIntent = new Intent(android.content.Intent.ACTION_SEND);
whatsappIntent.setType("image/*");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello World");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file)); //add image path
startActivity(Intent.createChooser(share, "Share image using"));
try {
    activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(activity, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
}    

【问题讨论】:

  • 那里没有任何东西可以将文本和媒体共享结合到whatsapp

标签: ios xcode swift whatsapp


【解决方案1】:

您可以在 WhatsApp 上发布图片或文字。但是,您不能同时发布两者,因为 whatsapp 不提供任何 API,您可以添加标题和发布带有文本的图像。

现在有一个可用于与 WhatsApp 交互的 api:

http://www.whatsapp.com/faq/en/iphone/23559013

还可以找到以下有用的答案:

截至 2014 年 8 月 4 日,您可以使用该问题的第二个答案中提到的 UIDocumentInteractionController:Share image/text through WhatsApp in an iOS app

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您的 swift 3 共享图像代码版本:

    let image = myUIImageVariable
            let filename = "myimage.wai"
            let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, false)[0] as NSString
            var destinationPath = documentsPath.appending("/" + filename) as NSString
             destinationPath = destinationPath.expandingTildeInPath as NSString
    
            let fileUrl = NSURL(fileURLWithPath: destinationPath as String) as NSURL
            do{
                try UIImagePNGRepresentation(image!)?.write(to: fileUrl as URL, options: Data.WritingOptions.atomic)
            }
            catch {}
            let documentController = UIDocumentInteractionController(url: fileUrl as URL)
            documentController.delegate = self
            documentController.uti = "net.whatsapp.image"
            documentController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: false)
    

    即使只是分享图片似乎也不起作用,但可能会节省某人的时间

    【讨论】:

      猜你喜欢
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 2015-09-27
      • 2020-01-04
      • 2022-10-09
      • 2023-01-30
      相关资源
      最近更新 更多