카메라,갤러리,저장

//
//  NewItemViewController.swift
//  NextViewEx
//
//  Created by hwan15 on 2017. 7. 18..
//  Copyright © 2017년 maxted. All rights reserved.
//

import UIKit

class NewItemViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{



    @IBOutlet weak var imagePicked: UIImageView!

    var imagePicker: UIImagePickerController!


    //MARK: - Take image
    @IBAction func takePhoto(_ sender: UIButton) {
        openCamera()
    }

    //MARK: - Take image
    @IBAction func takePhotoLib(_ sender: UITapGestureRecognizer) {

        openGallary()
    }


    //MARK: - Saving Image here
    @IBAction func save(_ sender: AnyObject) {
        UIImageWriteToSavedPhotosAlbum(imagePicked.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
    }

    func openGallary()
    {
        imagePicker.delegate = self
        imagePicker.allowsEditing = false
        imagePicker.sourceType = .photoLibrary
        present(imagePicker, animated: true, completion: nil)
    }


    func openCamera()
    {
        imagePicker.delegate = self
        imagePicker.sourceType = .camera
        present(imagePicker, animated: true, completion: 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)
        imagePicked.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    }




    override func viewDidLoad() {
        super.viewDidLoad()

        imagePicker =  UIImagePickerController()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




}

results matching ""

    No results matching ""