     
     // Selected image
     var selctedImage: UIImage!
-    
+
     
     // message label
     @IBOutlet var messageLabel: UILabel!

    // Outlet & action - save button
    @IBOutlet var saveButton: UIBarButtonItem!
    @IBAction func saveButtonAction(sender: UIBarButtonItem) {
        
        UIImageWriteToSavedPhotosAlbum(self.previewImageView.image!, nil, nil, nil)
        
        print("Image Saved")
    }
    
    

        // 3 - set source image
        myFilter?.setValue(sourceImage, forKey: kCIInputImageKey)
        
        // 4 - create core image context
        let context = CIContext(options: nil)
        
        // 5 - output filtered image as cgImage with dimension.
        let outputCGImage = context.createCGImage(myFilter!.outputImage!, fromRect: myFilter!.outputImage!.extent)

        // 6 - convert filtered CGImage to UIImage
        let filteredImage = UIImage(CGImage: outputCGImage)

        // 7 - set filtered image to preview
        self.previewImageView.image = filteredImage
    }
    
    
    // save imaage to photo gallery
    private func saveImageToPhotoGallery(){
        // Save image
        dispatch_async(dispatch_get_main_queue() ) {
            UIImageWriteToSavedPhotosAlbum(self.previewImageView.image!, self, "image:didFinishSavingWithError:contextInfo:", nil)
        }
    }
    
    
    // show message after image saved to photo gallery.
    func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
        
        // show success or error message.
        if error == nil {
            self.showAlertMessage(alertTitle: "Success", alertMessage: "Image Saved To Photo Gallery")
        } else {
            self.showAlertMessage(alertTitle: "Error!", alertMessage: (error?.localizedDescription)! )
        }
        
    }
    
    
