UIActivityViewController无法共享图像

时间:2018-06-09 12:44:18

标签: ios swift

我以这种方式分享图像

if let image = viewModels[indexPath.row].image, let viewController = viewController {
    let objectsToShare = [image]
    let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
    viewController.present(activityVC, animated: true, completion: nil)
}

在我的设备上,它可以正常工作。但在另一台设备上,我看到了下一个行为:

1)图像正确显示在屏幕上

2)出现活动控制器

3)当用户尝试将图像共享到应用程序(例如,电报或whatsapp)时,活动控制器显示人员列表但是当他选择共享目标时,控制器冻结一秒然后消失(图像没有)被送到目标)

4)将图像保存到照片工作正常。

这是什么?例如,内部IOS错误,或者UIImage实例有什么问题?两台设备上的IOS版本11.4

2 个答案:

答案 0 :(得分:0)

当尝试共享空文本文件时,我有相同的行为。在仅过滤非空文本之后,我开始启用发送到电报和whatsapp的功能。看来,当您尝试共享长度为0字节的内容时,会引发错误。请检查您的图像。

答案 1 :(得分:0)

将图片共享到Messenger或聊天工具时,请尝试以下代码

对我有用

func share(withUiImage uiImage : UIImage?){

    if let safeImage: UIImage = uiImage,let imagePngData = safeImage.pngData() {

        let activityViewController  = UIActivityViewController(activityItems: ["Share Image",imagePngData], applicationActivities: nil)

        if let popoverController = activityViewController.popoverPresentationController {
            popoverController.sourceView = self.view
            popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.maxY, width: 0, height: 0)
            popoverController.permittedArrowDirections = []
        }


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


    }
}