How can I show Stickers from Document directory in Swift

送分小仙女□ 提交于 2019-12-13 00:35:08

问题


    DispatchQueue.main.async
        {
            var x : Int = 0
            for item in self.arrGifs
            {
                let gif : GIF = item as! GIF
                let gifName = URL(string: gif.gifImage)?.lastPathComponent
                // let ur
                let pathUrl = URL.urlInDocumentsDirectory(with: "\(gifName!)").path

                print("image path is =====>", pathUrl)



                //RawImages.xcassets


                // if let url =  Bundle.main.url(forResource:"3", withExtension: "gif")
               //  {
                    print("url is ------>>>> ",url)
                    do
                    {
                        let sticker = try
                      //     MSSticker(contentsOfFileURL: url,localizedDescription: "")
                            MSSticker(contentsOfFileURL: URL(string:pathUrl)!,localizedDescription: "")



                        self.arrSticker.append(sticker)
                    }
                    catch
                    {
                        print(error.localizedDescription)
                    }
               //  }
            }
            self.createStickerBrowser()
    }

Well for the above " MSSticker(contentsOfFileURL: url,localizedDescription: "")" I can get stickers which are saved in Bundle. But If I give path of document directry, no sticker are shown. I am downloading the imges from api, saving in my sqlite(image name) and images in document directry so that user can view it Offline. I find no solutions . Need help what is wrong and how to do it right. Thank you.

     file:///private/var/containers/Bundle/Application/F8FF754B-F012-4B75-AA2E-9FA71846E6AB/Migos%20Lingo.app/PlugIns/MigosLingo.appex/3.gif === this from Bundle and I can view as Sticker

    /var/mobile/Containers/Data/PluginKitPlugin/DE11FB0E-89F9-4A65-917E-B4FEB8CA5470/Documents/111111-1508763742.gif   ===> Document directry and I cannot view as Stickers 

回答1:


Can you try this.

let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

After this make url because MSSticker create sticker with url path.Don't forget to append your image name with document path. after getting image path just do this.

let pathurl =  URL(fileURLWithPath: imagePath!)

now you can creat sticker.

MSSticker(contentsOfFileURL: pathurl, localizedDescription: "hello world")


来源:https://stackoverflow.com/questions/47406180/how-can-i-show-stickers-from-document-directory-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!