简体   繁体   English

使用背景图片和贴纸向 Instagram 分享故事 - IOS Swift

[英]Sharing a story to Instagram with a background image and a sticker - IOS Swift

I'm trying to share a story with a background image aa sticker image via URL Scheme on my ios app, i am using the attached code and it dose not work.我正在尝试在我的 ios 应用程序上通过 URL 方案与背景图像 aa 贴纸图像分享一个故事,我正在使用附加的代码,但它不起作用。 When i'm trying to share just a background image or just a sticker it does work.当我尝试只分享背景图片或贴纸时,它确实有效。 But when im trying share both a background image and a sticker in top of it, it dose not work.但是当我尝试在其顶部共享背景图像和贴纸时,它不起作用。

Any Ideas?有任何想法吗?

func shareToInstagram(deepLinkString : String){
        let url = URL(string: "instagram-stories://share")!
        if UIApplication.shared.canOpenURL(url){

            let backgroundData = UIImageJPEGRepresentation(UIImage(named: "shop_placeholder")!, 1.0)!
            let creditCardImage = UIImage(named: "share_instagram")!
            let stickerData = UIImagePNGRepresentation(creditCardImage)!
            let pasteBoardItems = [
                                    ["com.instagram.sharedSticker.backgroundImage" : backgroundData],
                                    ["com.instagram.sharedSticker.stickerImage" : stickerData],
                                  ]

            if #available(iOS 10.0, *) {

                UIPasteboard.general.setItems(pasteBoardItems, options: [.expirationDate: Date().addingTimeInterval(60 * 5)])
            } else {
                UIPasteboard.general.items = pasteBoardItems
            }
            UIApplication.shared.openURL(url)
        }

I copy pasted OP's code for use in my own app (only substituting different UIImages) and found only 1 issue, pasteboard items should be contained in a single array otherwise instagram will render only the first item (in this case the background layer).我复制粘贴的 OP 代码以在我自己的应用程序中使用(仅替换不同的 UIImages)并发现只有一个问题,粘贴板项目应包含在单个数组中,否则 instagram 将仅呈现第一个项目(在这种情况下为背景层)。 To fix this, replace the declaration of pasteboard items with the following code要解决此问题,请将粘贴板项目的声明替换为以下代码

let pasteBoardItems = [
                ["com.instagram.sharedSticker.backgroundImage" : backgroundData,
                "com.instagram.sharedSticker.stickerImage" : stickerData]
            ]

(basically just remove the close and open bracket separating the two items) (基本上只需删除分隔两个项目的关闭和打开支架)

Also as a previous answer stated, make sure "instagram-stories" is included in LSApplicationQueriesSchemes in the info.plist file同样如先前的答案所述,请确保 info.plist 文件中的 LSApplicationQueriesSchemes 中包含“instagram-stories”

I use this exact code in my app and it now works perfect我在我的应用程序中使用了这个确切的代码,现在它完美无缺

Everything is correct, my code is similar and it works for iOS 11+.一切正常,我的代码类似,适用于 iOS 11+。 I suggest you the following:我建议你以下几点:

  1. check the image data you pass to pasteboard (jpg can't be converted with UIImagePNGRepresentation and vice versa)检查您传递给粘贴板的图像数据(无法使用UIImagePNGRepresentation转换 jpg,反之亦然)
  2. check the info.plist.检查 info.plist。 You should enable "instagram-stories" scheme in it ( LSApplicationQueriesSchemes key)您应该在其中启用“instagram-stories”方案( LSApplicationQueriesSchemes键)
  1. Like Alec said, you need to put all of Instagram data in one list, not multiple lists.正如 Alec 所说,您需要将所有 Instagram 数据放在一个列表中,而不是多个列表中。 look at the example from the meta documents :查看元文档中的示例:
NSArray *pasteboardItems = @[@{@"com.instagram.sharedSticker.stickerImage" : stickerImage,
                               @"com.instagram.sharedSticker.backgroundTopColor" : backgroundTopColor,
                               @"com.instagram.sharedSticker.backgroundBottomColor" : backgroundBottomColor}];

2. For more recent readers, as of swift 4.2 and iOS 12 UIImageJPEGRepresentation is replaced by jpegData. 2. 对于最近的读者,从swift 4.2 和 iOS 12 UIImageJPEGRepresentation 开始被 jpegData 替换。 change改变

let backgroundData = UIImageJPEGRepresentation(yourImage, 1.0)

with

let backgroundData = yourImage.jpegData(compressionQuality: 1.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM