问题
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