class Gif: NSObject, NSCoding { let url: NSURL let videoURL: NSURL let caption: String? let gitImage: UIImage var gifData: NSData? init(url: NSURL, videoURL: NSURL, caption: String?){ self.url = url self.videoURL = videoURL self.caption = caption self.gifImage = UIImage.gifWithURL(url.absoluteString)! self.gifData = nil } required init?(coder decoder: NSCoder){ self.url = decoder.decodeObjectForKey("url") as! NSURL self.videoURL = decoder.decodeObjectForKey("videoURL") as! NSURL self.caption = decoder.decodeObjectForKey("caption") as? String self.gifImage = decoder.decodeObjectForKey("gifImage") as! UIImage self.gifData = decoder.decodeObjectForKey("gifData") as? NSData } func encodeWithCoder(coder: NSCoder){ coder.encodeObject(self.url, forKey: "url") coder.encodeObject(self.videoURL, forKey: "videoURL") coder.encodeObject(self.caption, forKey: "caption") coder.encodeObject(self.gifImage, forKey: "gifImage") coder.encodeObject(self.gifData, forKey:"gifData") } }
create the file path
var gifsFilePath: String { let directories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) let documentsPath = directories[0] let gifsPath = documentsPath.stringByAppendingString("/savedGifs") return gifsPath }
var gif: Gif? @IBOutlet weakl var gifImageView: UIImageView! override func viewDidLoad(){ super.viewDidLoad() gifImageView.image = gif?.gifImage } @IBAction func sharedGif(sendr: UIButton){ var itemsToShare = [NSData]() itemsToShare.append((self.gif?gifData)!) let activityVC = UIActivityViewController(activityItems: itemsToShare, applicationActivities: nil) activityVC.completionWithItemsHandler = {(activity, comleted, items, error) in: if (completed){ self.dismissViewControllerAnimated(true, completion: nil) } } presentViewController(activityVC, animated: true, completion:nil) }