我需要从我的应用程序中发送一个带有文本的图像,我知道如何只发送一个图像或只发送一个文本,但我不知道如何将它们结合起来。
只需一张图片:
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();
}
发布于 2015-08-21 17:02:18
您可以在WhatsApp上发布图像或文本。然而,你不能同时发布两者,因为whatsapp不提供任何可以添加字幕和发布带有文本的图片的API。
现在有了可以与WhatsApp交互的接口:
也可以在下面找到有用的答案:
您可以使用2014年8月4日此问题的第二个答案中提到的UIDocumentInteractionController:
希望这能有所帮助。
发布于 2017-09-18 10:43:36
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)
即使只是分享一张图片,仍然看起来不起作用,但可能会节省某人的时间
https://stackoverflow.com/questions/31920041
复制相似问题