swift3 ios10

swift3 - Rápido Toma una foto y guárdala en la biblioteca de fotos.



ios10 (5)

De hecho, puedes agregarlo a tu código y se guardará:

UIImageWriteToSavedPhotosAlbum(/* your image */, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { imagePicker.dismiss(animated: true, completion: nil) imageTake.image = info[UIImagePickerControllerOriginalImage] as? UIImage }

He estado buscando y no he podido encontrar la respuesta a esto. Tengo un botón "tomar una foto" y cuando se presiona, abre la cámara, tomas una foto y cuando seleccionas "Usar foto", quiero que se guarde en la biblioteca de fotos.
Puedo hacer todo menos guardar en la biblioteca, ¿puede alguien ayudarme?

Este es el código que tengo para abrir la cámara:


En Swift 3.0

@IBOutlet weak var userPhotoImageView: UIImageView! var pickerController = UIImagePickerController() var imageView = UIImage()

crea tus salidas de UIImageView y UIImagePickerController

in viewDidLoad let imageTapGesture = UITapGestureRecognizer(target: self, action: #selector(tapUserPhoto(_:))) imageTapGesture.delegate = self userPhotoImageView.addGestureRecognizer(imageTapGesture) imageTapGesture.numberOfTapsRequired = 1 userPhotoImageView.isUserInteractionEnabled = true pickerController.delegate = self func tapUserPhoto(_ sender: UITapGestureRecognizer){ let alertViewController = UIAlertController(title: "", message: "Choose your option", preferredStyle: .actionSheet) let camera = UIAlertAction(title: "Camera", style: .default, handler: { (alert) in self.openCamera() }) let gallery = UIAlertAction(title: "Gallery", style: .default) { (alert) in self.openGallary() } let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (alert) in } alertViewController.addAction(camera) alertViewController.addAction(gallery) alertViewController.addAction(cancel) self.present(alertViewController, animated: true, completion: nil) } func openCamera() { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) { pickerController.delegate = self self.pickerController.sourceType = UIImagePickerControllerSourceType.camera pickerController.allowsEditing = true self .present(self.pickerController, animated: true, completion: nil) } else { let alertWarning = UIAlertView(title:"Warning", message: "You don''t have camera", delegate:nil, cancelButtonTitle:"OK", otherButtonTitles:"") alertWarning.show() } } func openGallary() { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) { pickerController.delegate = self pickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary pickerController.allowsEditing = true self.present(pickerController, animated: true, completion: nil) } } public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { imageView = info[UIImagePickerControllerEditedImage] as! UIImage userPhotoImageView.contentMode = .scaleAspectFill userPhotoImageView.image = imageView dismiss(animated:true, completion: nil) } public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { print("Cancel") }

Aquí estoy usando tapgesture para hacer clic en UIImageView

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 } }


La respuesta de actualizada para Swift 4

class ImageCaptureViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate { @IBOutlet weak var takeImage: UIImageView! var imagePicker: UIImagePickerController! @IBAction func takePhoto(_ sender: UIButton) { imagePicker = UIImagePickerController() imagePicker.delegate = self imagePicker.sourceType = .camera present(imagePicker, animated: true, completion: nil) } @IBAction func savePhoto(_ sender: UIButton) { UIImageWriteToSavedPhotosAlbum(takeImage.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) } @objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) { if let error = 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) } } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { imagePicker.dismiss(animated: true, completion: nil) takeImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage } }


Use el código de abajo para una imagen tomada de la Galería fotográfica y guarde dentro de la biblioteca de fotos.

Soporte de código para la versión Swift 3.1 y 4.0 :

Primero, tenemos que hacer la configuración de los permisos dentro del archivo .plist del proyecto: -

1) cámara

<key>NSCameraUsageDescription</key> <string>This app will use camera.</string>

2) Fototeca

<key>NSPhotoLibraryUsageDescription</key> <string>You can select photos to attach to reports.</string>

3) Guardar en Photo Library

<key>NSPhotoLibraryAddUsageDescription</key> <string>Please allow access to save photo in your photo library</string>

Necesitamos abrir el archivo .pilst como un tipo de código fuente y luego agregar permisos dentro de -

Después de esto

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 Código de actualización -

class PhotoViewController: UIViewController, UIGestureRecognizerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate


class ImageCaptureViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate { @IBOutlet weak var takeImage: UIImageView! var imagePicker: UIImagePickerController! @IBAction func takePhoto(_ sender: UIButton) { imagePicker = UIImagePickerController() imagePicker.delegate = self imagePicker.sourceType = .camera present(imagePicker, animated: true, completion: nil) } @IBAction func savePhoto(_ sender: UIButton) { UIImageWriteToSavedPhotosAlbum(takeImage.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) } @objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) { if let error = 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) } } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { imagePicker.dismiss(animated: true, completion: nil) takeImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage } }