将下面的代码用于从照片库拍摄的图像并保存在照片库中。
文件中为权限进行设置:-Swift 3.1和4.0]版本的代码支持:
首先,我们必须在Project的。plist
1)相机
<key>NSCameraUsageDescription</key> <string>This app will use camera.</string>
2)图片库
<key>NSPhotoLibraryUsageDescription</key> <string>You can select photos to attach to reports.</string>
3)保存到照片库
类型打开。pilst文件,然后在-内添加permissions<key>NSPhotoLibraryAddUsageDescription</key> <string>Please allow access to save photo in your photo library</string>
我们需要以源代码
import UIKit
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var imageTake: UIImageView!
var imagePicker: UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: - Take image
@IBAction func takePhoto(_ sender: UIButton) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
//MARK: - Saving Image here
@IBAction func save(_ sender: AnyObject) {
UIImageWriteToSavedPhotosAlbum(imageTake.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
//MARK: - Add image to Library
func image(_ image: UIImage, didFinishSavingWithError error: Error?, 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: "Your altered image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
//MARK: - Done image capture here
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker.dismiss(animated: true, completion: nil)
imageTake.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
}
Swift 4.2代码更新-
class ViewController: UIViewController, UINavigationControllerDelegate { @IBOutlet weak var imageTake: UIImageView! var imagePicker: UIImagePickerController! enum ImageSource { case photoLibrary case camera } override func viewDidLoad() { super.viewDidLoad() } //MARK: - Take image @IBAction func takePhoto(_ sender: UIButton) { guard UIImagePickerController.isSourceTypeAvailable(.camera) else { selectImageFrom(.photoLibrary) return } selectImageFrom(.camera) } func selectImageFrom(_ source: ImageSource){ imagePicker = UIImagePickerController() imagePicker.delegate = self switch source { case .camera: imagePicker.sourceType = .camera case .photoLibrary: imagePicker.sourceType = .photoLibrary } present(imagePicker, animated: true, completion: nil) } //MARK: - Saving Image here @IBAction func save(_ sender: AnyObject) { guard let selectedImage = imageTake.image else { print("Image not found!") return } UIImageWriteToSavedPhotosAlbum(selectedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) } //MARK: - Add image to Library @objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) { if let error = error { // we got back an error! showAlertWith(title: "Save error", message: error.localizedDescription) } else { showAlertWith(title: "Saved!", message: "Your image has been saved to your photos.") } } func showAlertWith(title: String, message: String){ let ac = UIAlertController(title: title, message: message, preferredStyle: .alert) ac.addAction(UIAlertAction(title: "OK", style: .default)) present(ac, animated: true) } } extension ViewController: UIImagePickerControllerDelegate{ func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){ imagePicker.dismiss(animated: true, completion: nil) guard let selectedImage = info[.originalImage] as? UIImage else { print("Image not found!") return } imageTake.image = selectedImage } }
Anand Nimje的答案已更新为Swift 4
在Swift 3.0
import UIKit
class photoPickerController: UIViewController,UINavigationControllerDelegate{
@IBOutlet weak var imageTake: UIImageView!
var imagePicker: UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func takePhoto(_ sender: UIButton) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
@IBAction func saveToLibrary(_ sender: AnyObject) {
UIImageWriteToSavedPhotosAlbum(imageTake.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
}
extension photoPickerController : UIImagePickerControllerDelegate {
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
present(alert, animated: true)
} else {
let alert = UIAlertController(title: "Saved!", message: "Image saved successfully", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
present(alert, animated: true)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker.dismiss(animated: true, completion: nil)
imageTake.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
}
实际上您可以将其添加到您的代码中,它将保存: