Swift UIActivityViewController 图片分享无法工作

18

我正在使用UIActivityViewController分享图片。在WhatsApp最近的更改允许分享后,我能够在分享选项中看到WhatsApp。我分享了一张图片和一条消息,我可以看到文本消息,但是我无法分享图片。相同的代码与Viber、FB、Twitter等一起使用时都能正常工作,不确定我错过了什么关于WhatsApp的。

func shareImage() {
    var messageStr:String  = "Check out my awesome photo!"
    var img: UIImage = currentPhoto!

    var shareItems:Array = [img, messageStr]

    let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]

    self.presentViewController(activityViewController, animated: true, completion: nil)

}

WhatsApp 常见问题解答 http://www.whatsapp.com/faq/en/iphone/23559013 - Daniel Storm
我希望这些是早期的技术,因为我想使用iOS 8共享扩展WhatsApp上周推出。我的问题是我能够分享messageStr但无法分享UIImage img。 - Dinesh Jeyasankar
1
当“shareItems”数组仅包含“img”时,似乎只能共享图像。所以我认为这取决于WhatsApp。 - Satachito
谢谢,让我试试这个。 - Dinesh Jeyasankar
非常感谢,Satachito。看起来WhatsApp不支持同时包含文本和图片的数组。分享一下对我有效的修改。 - Dinesh Jeyasankar
嗨@DineshJeyasankar,当我分享带有透明背景的png图像时,WhatsApp会为我的图像添加黑色遮罩背景。是否有任何方法可以解决这个问题,以便删除这样的背景,或者我必须自己添加白色背景?在添加白色背景之前,请问您的想法。谢谢。 - Rajan Maharjan
3个回答

24

看起来 WhatsApp 只在数组中包含图片时分享图片,而不是图文混排。

func shareImage() {
    //var messageStr:String  = "Check out my awesome iPicSafe photo!"
    var img: UIImage = currentPhoto!
    //var shareItems:Array = [img, messageStr]
    var shareItems:Array = [img]
    let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]
    self.presentViewController(activityViewController, animated: true, completion: nil)
}

有没有一种方法可以同时添加 . 和 ?? ? - Amulya
不,这就是现在 WhatsApp 的工作方式。他们可能会在未来进行更改。 - Dinesh Jeyasankar
我想分享我的应用程序的标志,但图片会变大,并且有时也会被裁剪。在分享到Facebook/Whatsapp/Twitter/邮件时是否有适合的图片尺寸? - Sushree Swagatika
我正在尝试使用UIActivityViewController在Facebook上发布我的图片,但是以上代码无法正常工作。请建议。 - Anand Gautam

6

想要分享文字或图片?试试这个Swift

func share(shareText shareText:String?,shareImage:UIImage?){

    var objectsToShare = [AnyObject]()

    if let shareTextObj = shareText{
        objectsToShare.append(shareTextObj)
    }

    if let shareImageObj = shareImage{
        objectsToShare.append(shareImageObj)
    }

    if shareText != nil || shareImage != nil{
        let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.view

        presentViewController(activityViewController, animated: true, completion: nil)
    }else{
        print("There is nothing to share")
    }
}

要分享,只需这样调用:

let imageToShare = UIImage(named: "myImage")
share(shareText: "Sharing this text", shareImage: imageToShare)

3
建议将 shareText != nil || shareImage != nil 更改为 objectsToShare.count > 0 - Tim Chen
2
Zaid Pathan --- 对我不起作用,它只能分享文本或只能分享图像,两者都不行... 我已经尝试过Facebook和WhatsApp。 - bLacK hoLE
你能检查一下 shareTextshareImage 都不是 nil 吗? - Mohammad Zaid Pathan
@ZaidPathan 是的,它们不是nil,我搜索了一下并发现WhatsApp不支持图文混排。您可以在像iMessage和电子邮件这样的应用中尝试。感谢您的支持。 - bLacK hoLE
我正在尝试使用UIActivityViewController将我的图片发布到Facebook,但使用以上代码无效。请建议。 - Anand Gautam
显示剩余3条评论

0

在Swift 3中使用以下代码

 @IBAction func whatsappShareWithImages(_ sender: AnyObject)
    {

        let urlWhats = "whatsapp://app"
        if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) {
            if let whatsappURL = URL(string: urlString) {

                if UIApplication.shared.canOpenURL(whatsappURL as URL) {

                    if let image = UIImage(named: "whatsappIcon") {
                        if let imageData = UIImageJPEGRepresentation(image, 1.0) {
                            let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")
                            do {
                                try imageData.write(to: tempFile, options: .atomic)
                                self.documentInteractionController = UIDocumentInteractionController(url: tempFile)
                                self.documentInteractionController.uti = "net.whatsapp.image"
                                self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)

                            } catch {
                                print(error)
                            }
                        }
                    }

                } else {
                    // Cannot open whatsapp
                }
            }
        }

    }

将以下代码添加到您的应用程序“plist”中。
  <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>whatsapp</string>
    </array>

您还可以参考小应用程序:https://github.com/nithinbemitk/iOS-Whatsapp-Share


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接