使用Whatsapp发送图像和文本 [英] Send Image and Text With Whatsapp

查看:1097
本文介绍了使用Whatsapp发送图像和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的应用程序发送带有文本的图像,我知道如何仅发送图像或仅发送文本,但我不知道如何将它们组合在一起。

I need to send an image from my app with a text, I know how to send just an image or just a text, but I don't know how to combine both of them.

只是一张图片:

    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!)
    }    

如何发送带有文字的图像?

How can I send an image with a text?

编辑#1

我找到了一个与whatsapp共享文本图像的代码,但它在java中,你可以把它翻译成swift吗?

I found a code that share an image with text to whatsapp but it's in java, can you translate it to 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上。但是你不能一次发布两个,因为whatsapp没有提供任何你可以添加标题的API和带文字的图片。

You can post Image or Text on WhatsApp. However you can't post both at a time as whatsapp does not provide any API that you can add caption and post image with text.

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

Now there is an api available for interacting with WhatsApp:

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

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

另请参阅以下有用的答案:

Also Find below helpful answer:

自2014年8月4日起,您可以使用UIDocumentInteractionController,如第2个答案中所述: 通过WhatsApp在iOS应用程序中共享图像/文字

You can use the UIDocumentInteractionController as mentioned in the 2nd answer to this question as of August 4, 2014: Share image/text through WhatsApp in an iOS app

希望这会有所帮助。

这篇关于使用Whatsapp发送图像和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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