Sharing Gifs Swift

白昼怎懂夜的黑 提交于 2020-01-04 02:48:08

问题


How would I share a Gif in Swift? The method I am using right now to share the Gif URL only shares one image and does not share the animated Gif image. Here is what I have right now:

        var cell = self.collectionView.cellForItemAtIndexPath(index)
        println(index.item)
        var URLString: String = contentArray[index.item].contentUrlStirng
        var shareURL: NSURL = NSURL(string: "\(URLString)")!
        var shareImage: UIImage = UIImage.animatedImageWithAnimatedGIFURL(shareURL)
        let firstActivityItem: Array = [shareImage]
        let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: firstActivityItem, applicationActivities: nil)
        self.presentViewController(activityViewController, animated: true, completion: nil)

The URLString contains a .gif link. Would anyone know how to share the Gif image?


回答1:


I have figured out the answer. You don't have to share the actual image. To share a gif all you do is share the data as such:

 var cell = self.collectionView.cellForItemAtIndexPath(index)
        println(index.item)
        var URLString: String = contentArray[index.item].contentUrlStirng
        var shareURL: NSURL = NSURL(string: "\(URLString)")!
        var shareData: NSData = NSData(contentsOfURL: shareURL)!
        let firstActivityItem: Array = [shareData]
        let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: firstActivityItem, applicationActivities: nil)
        self.presentViewController(activityViewController, animated: true, completion: nil)

Happy Coding!




回答2:


For Swift 4.2 to share GIF Image using UIActivityViewController

let objPhoto = self.arrGiphy[indexPath.row]
let shareURL: NSURL = NSURL(string: "\(objPhoto.gifImageFixedHeightURL)")!
let shareData: NSData = NSData(contentsOf: shareURL as URL)!
let gifData:  [Any] = [shareData as Any]

let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: gifData, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)


来源:https://stackoverflow.com/questions/31765148/sharing-gifs-swift

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