swift Take a photo and save to photo library

2020-01-25 04:16发布

I have been searching and unable to find the answer to this. I have a button "take a photo" and when pressed, it opens the camera, you take a photo and when you select "Use Photo", I want it saved to the photo library.
I am able to do all but save to the library, can someone help?

this is the code I have to open the camera: enter image description here

标签: swift3 ios10
5条回答
贼婆χ
2楼-- · 2020-01-25 04:43
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
    }
}
查看更多
霸刀☆藐视天下
3楼-- · 2020-01-25 04:46

In fact You can just add it to your code and it will save:

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

}
查看更多
迷人小祖宗
4楼-- · 2020-01-25 04:54

In Swift 3.0

class PhotoViewController: UIViewController, UIGestureRecognizerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate 

create your UIImageView outlets and UIImagePickerController

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

Here i am using tapgesture for clicking on UIImageView

     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")
}
查看更多
▲ chillily
5楼-- · 2020-01-25 05:01

Use below code for an image taken from Photo Gallery and save inside photo library.

Code Support for Swift 3.1 & 4.0 version:

First, we have to do the setup for Permissions inside Project's .plist file:-

1) Camera

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

2) Photo Library

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

3) Save to Photo Library

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

We need to open .pilst file as a Source code type then add permissions inside -

open .plist file


After That


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 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
        }
        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
    }
}
查看更多
狗以群分
6楼-- · 2020-01-25 05:04

Anand Nimje's answer updated for 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
}
}
查看更多
登录 后发表回答