ios 에서 camera 와 photoLibrary 에 접근하는 예제이다.



Main.stroyborad 파일에서 이미지 뷰 하나와 버튼 두개를 만들고 controller.swift 파일에 연동한다.




아래는 ViewController.swift 파일의 전체 코드이다.


import UIKit

import MobileCoreServices


class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    

    

    @IBOutlet weak var imageView: UIImageView!

    

    var newMedia : Bool?

    

    @IBAction func useCamera(sender: AnyObject) {

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {

            let imagePicer = UIImagePickerController()

            imagePicer.delegate = self

            imagePicer.sourceType = UIImagePickerControllerSourceType.Camera

            imagePicer.mediaTypes = [kUTTypeImage as String]

            imagePicer.allowsEditing = false

            

            self.presentViewController(imagePicer, animated: true, completion: nil)

            newMedia = true

        }

    }

    

    @IBAction func useCameraRoll(sender: AnyObject) {

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum) {

            let imagePicker = UIImagePickerController()

            imagePicker.delegate = self

            imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

            imagePicker.allowsEditing = false

            

            self.presentViewController(imagePicker, animated: true, completion: nil)

            newMedia = false

        }

    }

    

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        let mediaType = info[UIImagePickerControllerMediaType] as! NSString

        self.dismissViewControllerAnimated(true, completion: nil)

        if mediaType.isEqualToString(kUTTypeImage as String) {

            let image = info[UIImagePickerControllerOriginalImage] as! UIImage

            imageView.image = image

            

            if(newMedia == true) {

                UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:" , nil)

            } else if mediaType.isEqualToString(kUTTypeMovie as String) {

                

            }

        }

    }

    

    func image(image:UIImage, didFinishSavingWithError error:NSErrorPointer, contextInfo:UnsafePointer<Void>) {

        

        if error != nil {

            let alert = UIAlertController(title: "Save Failed", message: "Failed to save image", preferredStyle: UIAlertControllerStyle.Alert)

            let cancelAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)

            alert.addAction(cancelAction)

            self.presentViewController(alert, animated: true, completion: nil)

        }

    }

    

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {

        self.dismissViewControllerAnimated(true, completion: nil)

    }


    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }


}







위의 
코드는  "핵심만 골라 배우는 ios 9 프로그래밍 (닐스미스 지음/ 황반석 옮김)" 에 있는 예제를 참고하여 작성하였습니다.

+ Recent posts