15장 카메라와 포토 라이브러리에서 미디어 가져오기



개발자 계정이 있어야 되서 동영상 녹화 못함, 시뮬레이션으로 하면 아래와 같이 에러 발생

 




//

//  ViewController.swift

//  CameraPhotoLibrary

//

//  Created by stayfoolish on 22/10/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit

import MobileCoreServices


class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {


    @IBOutlet var imgView: UIImageView!

    

    let imagePicker: UIImagePickerController! = UIImagePickerController()

    var captureImage: UIImage!

    var videoURL: URL!

    var flagImageSave = false

    

    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.

    }

    @IBAction func btnCaptureImageFromCamera(_ sender: UIButton) {

        if (UIImagePickerController.isSourceTypeAvailable(.camera)){

            flagImageSave = true

            

            imagePicker.delegate = self

            imagePicker.sourceType = .camera

            imagePicker.mediaTypes = [kUTTypeImage as String]

            imagePicker.allowsEditing = false

            

            present(imagePicker, animated: true, completion: nil)

        } else {

            myAlert("Camera inaccessable", message: "Application cannot access the camera.")

        }

    }

    @IBAction func btnLoadImageFromLibrary(_ sender: UIButton) {

        if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)){

            flagImageSave = false

            

            imagePicker.delegate = self

            imagePicker.sourceType = .photoLibrary

            imagePicker.mediaTypes = [kUTTypeImage as String]

            imagePicker.allowsEditing = true

            

            present(imagePicker, animated: true, completion: nil )

        }else {

            myAlert("Photo album inaccessable", message: "Application cannot access the photo album.")

        }

    }

    @IBAction func btnRecordVideoFromCamera(_ sender: UIButton){

        if (UIImagePickerController.isSourceTypeAvailable(.camera)){

            flagImageSave = true

            

            imagePicker.delegate = self

            imagePicker.sourceType = .camera

            imagePicker.mediaTypes = [kUTTypeImage as String]

            imagePicker.allowsEditing = false

            

            present(imagePicker, animated: true, completion: nil)

        }else {

            myAlert("Camera inaccessable", message: "Application cannout access the camera.")

        }

        

    }

    @IBAction func btnLoadVideoFromLibrary(_ sender: UIButton){

        if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)){

            flagImageSave = false

            

            imagePicker.delegate = self

            imagePicker.sourceType = .photoLibrary

            imagePicker.mediaTypes = [kUTTypeImage as String]

            imagePicker.allowsEditing = false

            

            present(imagePicker, animated: true, completion: nil)

        }else {

            myAlert("Photo album inaccessable", message: "Application cannout access the photo album.")

        }

    }

    

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

        let mediaType = info[UIImagePickerControllerMediaType] as! NSString

        

        if mediaType.isEqual(to: kUTTypeImage as NSString as String){

            captureImage = info[UIImagePickerControllerOriginalImage] as! UIImage

            

            if flagImageSave {

                UIImageWriteToSavedPhotosAlbum(captureImage, self, nil, nil)

            }

            imgView.image = captureImage

        }else if mediaType.isEqual(to: kUTTypeMovie as NSString as String){

            if flagImageSave {

                videoURL = (info[UIImagePickerControllerMediaURL] as! URL)

                

                UISaveVideoAtPathToSavedPhotosAlbum(videoURL.relativePath, self, nil, nil)


            }

        }

        self.dismiss(animated: true, completion: nil)

    }

    

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

        self.dismiss(animated: true, completion: nil)

    }

    

    func myAlert(_ title: String, message: String){

        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

        let action = UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil)

        alert.addAction(action)

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

        

    }


}






16장 코어 그래픽스로 화면에 그림 그리기




//

//  ViewController.swift

//  DrawGraphics

//

//  Created by stayfoolish on 22/10/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


    @IBOutlet var imgView: UIImageView!

    

    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.

    }

    

    @IBAction func btnDrawLine(_ sender: UIButton){

        UIGraphicsBeginImageContext(imgView.frame.size)

        let context = UIGraphicsGetCurrentContext()!

        

        // Draw Line

        context.setLineWidth(2.0)

        context.setStrokeColor(UIColor.red.cgColor)

        

        context.move(to: CGPoint(x: 50, y: 50))

        context.addLine(to: CGPoint(x: 250, y: 250))

        

        context.strokePath()

        

        // Draw Triangle

        context.setLineWidth(4.0)

        context.setStrokeColor(UIColor.blue.cgColor)

        

        context.move(to: CGPoint(x: 150, y: 200))

        context.addLine(to: CGPoint(x: 250, y: 350))

        context.addLine(to: CGPoint(x: 50, y: 350))

        context.addLine(to: CGPoint(x: 150, y: 200))

        context.strokePath()

        imgView.image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

    }


    @IBAction func btnDrawRectangle(_ sender: UIButton){

        UIGraphicsBeginImageContext(imgView.frame.size)

        let context = UIGraphicsGetCurrentContext()!

        

        // Draw Rectangle

        context.setLineWidth(2.0)

        context.setStrokeColor(UIColor.red.cgColor)

        

        context.addRect(CGRect(x: 50, y: 100, width: 200, height: 200))

        context.strokePath()

        

        imgView.image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

    }

    

    

    @IBAction func btnDrawCircle(_ sender: UIButton){

        UIGraphicsBeginImageContext(imgView.frame.size)

        let context = UIGraphicsGetCurrentContext()!

        

        // Draw Ellipse

        context.setLineWidth(2.0)

        context.setStrokeColor(UIColor.red.cgColor)

        context.addEllipse(in: CGRect(x: 50, y: 50, width: 200, height: 100))

        context.strokePath()

        

        // Draw Circle

        context.setLineWidth(5.0)

        context.setStrokeColor(UIColor.green.cgColor)

        

        context.addEllipse(in: CGRect(x: 50, y: 200, width: 200, height: 200))

        context.strokePath()

        

        imgView.image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        

    }

    @IBAction func btnDrawArc(_ sender: UIButton){

        UIGraphicsBeginImageContext(imgView.frame.size)

        let context = UIGraphicsGetCurrentContext()!

        

        // Draw Arc

        context.setLineWidth(5.0)

        context.setStrokeColor(UIColor.red.cgColor)

        

        context.move(to: CGPoint(x: 50, y: 50))

        context.addArc(tangent1End: CGPoint(x: 200, y: 50), tangent2End: CGPoint(x: 200, y: 200), radius: CGFloat(50))

        context.addLine(to: CGPoint(x: 200, y: 200))

        

        context.move(to: CGPoint(x: 100, y: 250))

        context.addArc(tangent1End: CGPoint(x: 250, y: 250), tangent2End: CGPoint(x: 100, y: 400), radius: CGFloat(20))

        context.addLine(to: CGPoint(x: 100, y: 400))

        

        context.strokePath()

        

        imgView.image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

    }

    @IBAction func btnDrawFill(_ sender: UIButton){

        UIGraphicsBeginImageContext(imgView.frame.size)

        let context = UIGraphicsGetCurrentContext()!

        

        // Draw Rectangle

        context.setLineWidth(1.0)

        context.setStrokeColor(UIColor.red.cgColor)

        context.setFillColor(UIColor.red.cgColor)

        

        let rectangle = CGRect(x: 50, y: 50, width: 200, height: 100)

        context.addRect(rectangle)

        context.fill(rectangle)

        context.strokePath()

        

        // Draw Circle

        context.setLineWidth(1.0)

        context.setStrokeColor(UIColor.blue.cgColor)

        context.setFillColor(UIColor.blue.cgColor)

        

        let circle = CGRect(x: 50, y: 200, width: 200, height: 100)

        context.addEllipse(in: circle)

        context.fillEllipse(in: circle)

        context.strokePath()

        

        // Draw Triangle

        context.setLineWidth(1.0)

        context.setStrokeColor(UIColor.green.cgColor)

        context.setFillColor(UIColor.green.cgColor)

        

        context.move(to: CGPoint(x: 150, y: 350))

        context.addLine(to: CGPoint(x: 250, y: 450))

        context.addLine(to: CGPoint(x: 50, y: 450))

        context.addLine(to: CGPoint(x: 150, y: 350))

        context.fillPath()

        context.strokePath()

        

        imgView.image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        

    }

}







+ Recent posts