我在 stackoverflow 中使用 swift 3/4 进行搜索,以使用 UIActivityViewController 共享图像 instagram。但是,当我想分享图像然后单击 Instagram 徽标时,会出现警报,显示“我们很抱歉,但出了点问题。请重试。有办法解决这个问题吗?
let image = UIImage(named: "instaIcon.png")
let objectsToShare: [AnyObject] = [ image! ]
let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook, UIActivityType.addToReadingList, UIActivityType.assignToContact, UIActivityType.copyToPasteboard,
UIActivityType.mail, UIActivityType.markupAsPDF, UIActivityType.message, UIActivityType.openInIBooks,
UIActivityType.postToFacebook, UIActivityType.postToFlickr, UIActivityType.postToTencentWeibo,
UIActivityType.postToTwitter, UIActivityType.postToVimeo, UIActivityType.postToWeibo]
self.present(activityViewController, animated: true, completion: nil)
因此,对于我们未来的开发人员来说,当我尝试使用
UIActivityViewController
和 UIImage
分享帖子或故事时,我遇到了同样的问题,当我从 点击故事或帖子按钮时,它总是返回给我
Something went wrong
UIActivityViewController
。
所以解决方案是共享图像 URL 而不是 UIImage,基本上,只需将其保存到临时文件夹,然后共享 URL 就可以了。
guard let imageData = self.jpegData(compressionQuality: 0.5) else { return nil }
let tempDir = FileManager.default.temporaryDirectory
let url = tempDir.appendingPathComponent("anyName.jpg")
do {
try imageData.write(to: url)
vc.present(UIActivityViewController(activityItems: [url], applicationActivities: nil))
} catch {
return nil
}