Swift 4.2 , Xcode 10.0
let fileManager = FileManager.default
let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("scan.jpg")
DispatchQueue.global(qos: .background).async {
let image = UIImage(data: "Enter ImageData")
if image != nil {
let imageData = image!.pngData()
UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
fileManager.createFile(atPath: paths as String, contents: imageData, attributes: nil)
DispatchQueue.main.async {
let alert = UIAlertController(title: "Success", message: "Image have been saved successfully!", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: { (action) in
let _ = self.navigationController?.popViewController(animated: true)
}))
self.present(alert, animated: true, completion: nil)
}
}
else {
let alert = UIAlertController(title: "Message", message: "Image not found", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel, handler: { (action) in
}))
self.present(alert, animated: true, completion: nil)
}
}