Swift3 – Swift photo and saved to photo library

I have been looking for, but I can’t find the answer. I have a button “Take a picture”. When pressed, it turns on the camera and you take a picture. When you choose “Use photo”, I I hope it is saved to the photo library.
I can do it for everyone except saving to the library, can anyone help?

This is my code to open the camera:
enter image description here

Use the following code to get the image taken from the photo library and save it in the photo library.

Code Support for Swift 3.1 & 4.0 version:

First, we must set the permissions in the Project’s .plist file: –

1)Camera

NSCameraUsageDescription
This app will use camera.

2)Picture library

NSPhotoLibraryUsageDescription
You can select photos to attach to reports.

3) Save to photo library

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

We need to open the .pilst file as the source code type, and then add it Permission –

open .plist file

after

p>

import UIKi t

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
}< b r />
}

Swift 4.2 Code update –

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)
}< br />
func selectImageFrom(_ source: ImageSource){
imagePicker = UIImagePickerController()
imagePicker.delegate = self
switch source {
case .camera:< br /> im agePicker.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]){< br /> imagePicker.dismiss(animated: true, completion: nil)
guard let selectedImage = info[.originalImage] as? UIImage else {
print("Image not found!")
return
}
imageTake.image = selectedImage
}
}

I have been looking for it, but I can’t find the answer. I have a button “photograph”, when pressed, it turns on the camera, you take a photo, when you select “use photo”, I want it to be saved to the photo library.
I can do everything except save to the library People, can anyone help?

This is my code to open the camera:
enter image description here

Use the following code to get the image taken from the photo library and save it in the photo library.

Code Support for Swift 3.1 & 4.0 version:

First of all, we must be in Project. Set permissions in the plist file: –

1)Camera

NSCameraUsageDescription
This app will use camera.

2) Picture library

NSPhotoLibraryUsageDescription
You can select photos to attach to reports.

3) Save to photo library

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

We need to open the .pilst file as the source code type, and then add permissions in it-

open .plist file

after

 import UIKit

class ViewController: UIViewController, UINavigationContro llerDelegate, 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 Code update –

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

Leave a Comment

Your email address will not be published.