首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Whatsapp发送图像和文本

使用Whatsapp发送图像和文本
EN

Stack Overflow用户
提问于 2015-08-10 20:49:09
回答 2查看 9.8K关注 0票数 5

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

只需一张图片:

代码语言:javascript
运行
复制
    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)    

只有一段文字:

代码语言:javascript
运行
复制
    var whatsappURL = NSURL(string: "whatsapp://send?text=hello,%20world")

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

如何将图片与文本一起发送?

编辑#1

我发现了一个代码,可以将图片和文本共享到whatsapp,但它是java的,你能把它翻译成swift吗?

代码语言:javascript
运行
复制
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();
}    
EN

回答 2

Stack Overflow用户

发布于 2015-08-21 17:02:18

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

现在有了可以与WhatsApp交互的接口:

也可以在下面找到有用的答案:

您可以使用2014年8月4日此问题的第二个答案中提到的UIDocumentInteractionController:

希望这能有所帮助。

票数 4
EN

Stack Overflow用户

发布于 2017-09-18 10:43:36

swift 3的分享图片代码的一个版本:

代码语言:javascript
运行
复制
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)

即使只是分享一张图片,仍然看起来不起作用,但可能会节省某人的时间

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31920041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档