我需要从我的应用程序发送带有文本的图像,我知道如何仅发送图像或仅发送文本,但我不知道如何将它们组合在一起。
只是一张图片:
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();
}
答案 0 :(得分:3)
您可以在WhatsApp上发布图片或文字。但是你不能一次发布两个,因为whatsapp没有提供任何你可以添加标题的API和带文字的图像。
现在有一个api可用于与WhatsApp进行交互:
<强> http://www.whatsapp.com/faq/en/iphone/23559013 强>
另见以下有用的答案:
您可以使用自2014年8月4日起在此问题的第二个答案中提及的UIDocumentInteractionController: Share image/text through WhatsApp in an iOS app
希望这会有所帮助。
答案 1 :(得分:1)
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)
即使只是共享图片,似乎仍然无效,但可能会节省某人的时间