Swift - 4, Xcode 9, IOS 11
Info.plist
<key>NSPhotoLibraryUsageDescription</key>
<string> photos description.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string> photos add description.</string>
Добавить код при нажатии кнопки
let url = "https://www.xamarin.com/content/images/pages/branding/assets/xamagon.png"
let data = try! Data(contentsOf: URL(string: url)!)
let image:UIImage = UIImage(data: data)!
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
@objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Saved!", message: "The image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}