共享 Gif Swift

Sharing Gifs Swift

如何在 Swift 中分享 Gif?我现在使用的分享Gif的方法URL只分享一张图片,不分享动态Gif图片。这是我现在拥有的:

        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)

URLString 包含一个 .gif link。谁知道如何分享 Gif 图片?

我已经找到答案了。您不必共享实际图像。要共享 gif,您只需按如下方式共享数据:

 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)

编码愉快!

对于Swift4.2使用UIActivityViewController分享GIF图片

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)

Swift 5 完成代码

@IBAction func sharePressed(_ sender: Any) {
    guard let _ = tempImageView.image else {
      return
    }
if pathGif == nil {
     return
}
let shareURL: NSURL = pathGif! as NSURL
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

    activityViewController.excludedActivityTypes = [
       UIActivity.ActivityType.postToWeibo,
       UIActivity.ActivityType.print,
       UIActivity.ActivityType.assignToContact,
       UIActivity.ActivityType.saveToCameraRoll,
       UIActivity.ActivityType.addToReadingList,
       UIActivity.ActivityType.postToFlickr,
       UIActivity.ActivityType.postToVimeo,
       UIActivity.ActivityType.postToTencentWeibo
    ]
    self.present(activityViewController, animated: true, completion: nil)
}