iOS11:UIActivityViewController无法成功将UIImage共享给第三方应用程序 [英] iOS11: UIActivityViewController not successfully sharing UIImage to 3rd party apps

查看:51
本文介绍了iOS11:UIActivityViewController无法成功将UIImage共享给第三方应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此在iOS10中,如果您想与UIActivityViewController共享图像,则只需编写一些swift3代码,如下所示:

So in iOS10 if you wanted to share an image with UIActivityViewController, you would just need to write some swift3 code that looked like:

func shareTapped() {       
        if let image = imageView.image {
            let vc = UIActivityViewController(activityItems: [image], applicationActivities: [])
            vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
            present(vc, animated: true, completion: nil)
        }

如果您要将图像保存到相机胶卷(前提是您已经要求了适当的权限),或者想通过Messenger,Airdrop或任何其他Apple实施方式进行共享,则该方法仍然可以使用.

This method still appears to work if you want to save the image to your camera roll (providing you've asked for the appropriate permissions), or want to share via messenger, airdrop, or any other Apple implementation.

不幸的是,如果您尝试通过Twitter,Facebook,Instagram等共享图像,他们将无法成功附加图像,并且会抛出错误或无提示地失败.

Unfortunately if you attempt to share the image via Twitter, Facebook, Instagram, etc, they won't successfully attach the image and will either throw an error or fail silently.

我花了很多时间搜索google,youtube和stackoverflow,自iOS11发行以来,所有内容都指向iOS10解决方案.

I've spent a ton of time searching google, youtube and stackoverflow, and everything points to an iOS10 solution and nothing since iOS11 released.

我应该注意,如果您要在这些行上传递字符串,URL或其他内容,则此功能仍然可以正常工作.

I should note that this function still works fine if you want to pass a string, url, or something on those lines.

推荐答案

简短版/TLDR

通过先将图像转换为数据,然后将该对象传递给UIActivityViewController,解决了尝试传递图像时Twitter/Facebook的问题:

By converting your image into Data first, and then passing that object to the UIActivityViewController solves the Twitter / Facebook problem when trying to pass an image:

   if let jpgImage = UIImageJPEGRepresentation(image, 0.8) {
       let vc = UIActivityViewController(activityItems: [jpgImage], applicationActivities: [])
   }

更长的移植和完整的代码可以在下面找到:

Longer explantation and full code can be found below:

长版

我联系了几位专家,他们都暗示它应该可以工作,但是即使查看他们的示例应用程序,他们都遇到相同的问题.因此,我最终认为这可能是Twitter或Facebook的问题,因为与Apple应用程序共享才可以正常工作.然后,我开始使用照片"应用程序,并决定点击该处的共享"按钮,瞧,它可与Twitter和Facebook一起使用!这让我想知道这是否意味着需要一些增量设置才能使其正常运行.在尝试了许多不同的东西之后,我终于找到了一种可行的方法.通过将图像通过PNG或JPG转换为数据并将该新对象传递给您的activityItems,可以解决Twitter/Facebook问题,并且似乎仍然可以与其他所有内容一起使用.将图像转换为JPG的更新的swift4/ios11代码如下所示:

I reached out to a few experts who all implied it should just work, but even when looking at their sample apps, they all exhibited the same problem. So I ultimately thought this might be an issue with Twitter or Facebook since sharing to the Apple apps just worked. I then got around to playing with the Photos app and decided to hit the share button there, and voila, it worked with Twitter and Facebook! This made me wonder whether this meant there was some incremental setup needed to get it this to work. After trying many different things I finally found a method that worked. By converting the image to Data via a PNG or JPG and passing that new object to your activityItems solves the Twitter / Facebook problem and still seems to work with everything else. The updated swift4 / ios11 code converting the image to JPG would look like:

@objc func shareTapped() {
        if let image = imageView.image {
            if let jpgImage = UIImageJPEGRepresentation(image, 0.8) {
                let vc = UIActivityViewController(activityItems: [jpgImage], applicationActivities: [])
                // add the following line if using doing universal and need iPad
                vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
                present(vc, animated: true)
            }
        }
    }

希望这可以帮助自iOS11以来一直在努力共享图像的其他人.

Hope this helps anyone else that's been struggling sharing an image since iOS11.

这篇关于iOS11:UIActivityViewController无法成功将UIImage共享给第三方应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆