Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing Gifs Swift

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?

like image 662
Stefan DeClerck Avatar asked Mar 15 '23 05:03

Stefan DeClerck


2 Answers

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!

like image 187
Stefan DeClerck Avatar answered Mar 20 '23 16:03

Stefan DeClerck


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)
like image 31
Hardik Thakkar Avatar answered Mar 20 '23 14:03

Hardik Thakkar