UIImagePickerController를 활용하여, 사진촬영, 동영상 촬영, Library 가져오기

4개의 이용방안에 함수가 모양이 비슷합니다. 

 

전체 소스입니다.

import UIKit
import MobileCoreServices

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

    @IBOutlet weak 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.
    }
    
    func myAlert(_  title : String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        let action = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)
        
        alert.addAction(action)
        self.present(alert, animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let mediaType = info[UIImagePickerController.InfoKey.mediaType] as! NSString
        
        if mediaType.isEqual(to: kUTTypeImage as NSString as String) {
            captureImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
            
            if flagImageSave {
                UIImageWriteToSavedPhotosAlbum(captureImage, self, nil, nil)
            }
            
            imgView.image = captureImage
        } else if mediaType.isEqual(to: kUTTypeMovie as NSString as String) {
            videoURL = (info[UIImagePickerController.InfoKey.mediaURL] as! URL)
            
            UISaveVideoAtPathToSavedPhotosAlbum(videoURL.relativePath, self, nil, nil)
        }
        
        self.dismiss(animated: true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }


    @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("Phto 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 = [kUTTypeMovie as String]
            imagePicker.allowsEditing = false
            
            present(imagePicker, animated: true, completion: nil)
        } else {
            myAlert("Camera inaccessable", message: "Application cannot access the camera.")
        }
    }
    @IBAction func btnLoadVideoFromLibrary(_ sender: UIButton) {
        if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)) {
            flagImageSave = false
            
            imagePicker.delegate = self
            imagePicker.sourceType = .photoLibrary
            imagePicker.mediaTypes = [kUTTypeMovie as String]
            imagePicker.allowsEditing = true
            
            present(imagePicker, animated: true, completion: nil)
        } else {
            myAlert("Phto album inaccessable", message: "Application cannot access the photo album.")
        }
    }
}
Posted by 목표를 가지고 달린다
,

1. 이미지를 배치하고, "Aspect Fit"으로 설정하면, 사진이 찌그러지는 것을 막기 위해 비율에 맞게 표시할 수 있다. 다만 여백이 생길 수 있는데, 이때... "Background"으로 [System grouped Background Color] 색상을 설정하면, 이미지가 비율에 맞게 보일 때 남는 여백을 표시할 수 있다. 

2. 카메라 테스트는 iOS 시뮬레이터에서 할수 없어서, 기기에 옮겨서 해야 하는데, 이때 개발자 라이선스 등록이 되어야 한다. 카메라, GPS, 자이로센서, 조도 센서, 블루투스 등의 동작을 확인하려면 기기에서 직접 동작해 봐야 한다. 

3. 카메라 사용권한! [info.plist] 파일에서 [Main storyboard file base name]을 + 클릭한다. 그리고 Privcy라고 입력하면, 관련키 목록이 나타난다. 여기서 [Privacy - Camera Usage Description]을 선택한다. 같은 방법은
 - 마이크로폰 접근키 : Privacy - Microphone Usage Description
 - 포토라이브러리 저장키 : Privacy - Phto Libary Additions Usage Description
 - 포토라이브러리 접근키 : Privacy - Photo Library Usage Description  을 추가한다.
그러면, 다음 실행할때, 권한 승인 여부를 확인하는 창이 뜬다.  

4. 사진이나 비디오 촬영을 하거나 포토 라이브러리에서 선택을 끝났을 때, 호출되는 didFinishPickingMediaWithInfo()메소드를 구현하여, 선택한 이미지나 동영상이 재생되도록한다.

5. 사진이나 비디오 촬영을 취소하거나, 포토 라이브러리에서 선택을 하지 않았을 때, 호출되는 imagePickerControllerDidCancel()메소드를 구현하여, 선택한 이미지나 동영상이 재생되도록한다.

6. iOS 시뮬레이터에서 멀티 터치를 테스트하려면, option키를 눌러야 동시 터치가 가능합니다.

/**** 
	핀치 제스쳐를 이용해서 이미지와 폰트 크기를 변화시키는 예제코드
    시뮬레이터에서 테스트시 option키를 누른 상태에서 키패드를 조작해야 멀티-터치가 됨
****/
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var lblPinch: UILabel!
    
    var initialFontSize : CGFloat!
    
    @IBOutlet weak var imgView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let pinch = UIPinchGestureRecognizer(target: self, action: #selector(ViewController.doPinch(_:)))
        
        self.view.addGestureRecognizer(pinch)
        
        let pinchImage = UIPinchGestureRecognizer(target: self, action: #selector(ViewController.doPinchImage(_:)))
        
        self.view.addGestureRecognizer(pinchImage)
    }
    
    @objc func doPinch(_ pinch : UIPinchGestureRecognizer) {
        if pinch.state == UIPinchGestureRecognizer.State.began {
            initialFontSize = lblPinch.font.pointSize
        } else {
            lblPinch.font = lblPinch.font.withSize(initialFontSize * pinch.scale)
        }
    }
    
    
    @objc func doPinchImage(_ pinch : UIPinchGestureRecognizer) {
        imgView.transform = imgView.transform.scaledBy(x: pinch.scale, y: pinch.scale)
        pinch.scale = 1
    }


}
Posted by 목표를 가지고 달린다
,

스위프트 Swipe제스쳐 이벤트 따라하기

코디는 이벤트를 정의하고, 이벤트 실행시 수행할 함수를 작성하면 됩니다.

1. 먼저 아래와 같이 이벤트를 정의하고 등록합니다.(addGestureRecognizer)

        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeUp.direction = UISwipeGestureRecognizer.Direction.up
        self.view.addGestureRecognizer(swipeUp)

2. 이벤트를 정의할 때, 명시한 #selector에 들어가는 함수를 정의합니다.(이벤트 수행할 내용 정의)

@objc func respondToSwipeGuesture(_ gesture : UIGestureRecognizer) {
        if let swipeGuesture = gesture as? UISwipeGestureRecognizer {
            imgViewUp.image = imgUp[0]
            imgViewDown.image = imgDown[0]
            imgViewRight.image = imgRight[0]
            imgViewLeft.image = imgLeft[0]
            
            switch swipeGuesture.direction {
            case UISwipeGestureRecognizer.Direction.up :
                imgViewUp.image = imgUp[1]
            case UISwipeGestureRecognizer.Direction.down :
                imgViewDown.image = imgDown[1]
            case UISwipeGestureRecognizer.Direction.right :
                imgViewRight.image = imgRight[1]
            case UISwipeGestureRecognizer.Direction.left :
                imgViewLeft.image = imgLeft[1]
            default :
                break
            }
            
            
        }
    }

3. 테스트 

멀티 터치로 변경하려면, let numOfTouches = 2 를 선언한 후, 이벤트 등록할때 numberOfTouchesRequired의 속성에 대입하면 됩니다. iOS시뮬레이터에서 테스트 할 경우, 'option' 키를 누르면 멀티 터치가 가능합니다. 다만, 두개의 터치가 기본값으로 반대 방향으로 이동하므로, 'option'키와 'shift' 키를 동시에 누르면서 이동하면 제대로 멀티 터치 - Swipe 테스트를 할 수 있습니다.

전체 소스입니다. 

// Swipe를 구현하고, 멀티터치를 인지할 수있음
// 싱글터치와 멀티터치를 구분하려면, 이소스 말고, 다음의 멀티터치 소스 확인 필요


import UIKit

class ViewController: UIViewController {
    
    /* 멀티터치를 스와이프를 하려면, numberOfTouchesRequired 속성이 필요한데,
     이속석에 numOfTouches = 2 의 값을 대입해야 합니다.
     시뮬레이터에서 'option' 키를 누르고 테스트하면, 두개의 동그라미가 반대로 향하므로
     option키와 shift 키를 동시에 누르면서 움직이면, 멀티 터치가 됩니다.*/
    
    let numOfTouches = 2

    @IBOutlet weak var imgViewUp: UIImageView!
    @IBOutlet weak var imgViewDown: UIImageView!
    @IBOutlet weak var imgViewLeft: UIImageView!
    @IBOutlet weak var imgViewRight: UIImageView!
    
    
    var imgLeft = [UIImage] ()
    var imgRight = [UIImage] ()
    var imgUp = [UIImage] ()
    var imgDown = [UIImage] ()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        imgUp.append(UIImage(named: "arrow.up.png")!)
        imgDown.append(UIImage(named: "arrow.down.png")!)
        imgRight.append(UIImage(named: "arrow.right.png")!)
        imgLeft.append(UIImage(named: "arrow.left.png")!)
        
        imgUp.append(UIImage(named: "arrow.up.black.png")!)
        imgDown.append(UIImage(named: "arrow.down.black.png")!)
        imgRight.append(UIImage(named: "arrow.right.black.png")!)
        imgLeft.append(UIImage(named: "arrow.left.black.png")!)
        
        imgViewUp.image = imgUp[0]
        imgViewDown.image = imgDown[0]
        imgViewRight.image = imgRight[0]
        imgViewLeft.image = imgLeft[0]
        
        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeUp.direction = UISwipeGestureRecognizer.Direction.up
        // multi-touch swipe
        swipeUp.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeUp)
        
        let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeDown.direction = UISwipeGestureRecognizer.Direction.down
        // multi-touch swipe
        swipeDown.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeDown)
        
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeRight.direction = UISwipeGestureRecognizer.Direction.right
        // multi-touch swipe
        swipeRight.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeRight)
        
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
        // multi-touch swipe
        swipeLeft.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeLeft)
         
    }
    
    @objc func respondToSwipeGuesture(_ gesture : UIGestureRecognizer) {
        if let swipeGuesture = gesture as? UISwipeGestureRecognizer {
            imgViewUp.image = imgUp[0]
            imgViewDown.image = imgDown[0]
            imgViewRight.image = imgRight[0]
            imgViewLeft.image = imgLeft[0]
            
            switch swipeGuesture.direction {
            case UISwipeGestureRecognizer.Direction.up :
                imgViewUp.image = imgUp[1]
            case UISwipeGestureRecognizer.Direction.down :
                imgViewDown.image = imgDown[1]
            case UISwipeGestureRecognizer.Direction.right :
                imgViewRight.image = imgRight[1]
            case UISwipeGestureRecognizer.Direction.left :
                imgViewLeft.image = imgLeft[1]
            default :
                break
            }
            
            
        }
    }


}

 

5. 멀티 터치와 싱글 터치 구분

위의 예제는 numberOfTouchesRequired에 값을 2를 대입하여, 싱글 또는 멀치 터치를 인식하는 것이라면,  
아래의 소스는 멀티 터치에 대한 이벤트, 싱글터치에 대한 이벤트를 각각 등록하고 정의한 예입니다. 

// 멀티터치와 싱글터치를 구분할 수 있는 Swipe 예제.

import UIKit

class ViewController: UIViewController {
    
    /* 멀티터치를 스와이프를 하려면, numberOfTouchesRequired 속성이 필요한데,
     이속석에 numOfTouches = 2 의 값을 대입해야 합니다.
     시뮬레이터에서 'option' 키를 누르고 테스트하면, 두개의 동그라미가 반대로 향하므로
     option키와 shift 키를 동시에 누르면서 움직이면, 멀티 터치가 됩니다.*/
    
    let numOfTouches = 2

    @IBOutlet weak var imgViewUp: UIImageView!
    @IBOutlet weak var imgViewDown: UIImageView!
    @IBOutlet weak var imgViewLeft: UIImageView!
    @IBOutlet weak var imgViewRight: UIImageView!
    
    
    var imgLeft = [UIImage] ()
    var imgRight = [UIImage] ()
    var imgUp = [UIImage] ()
    var imgDown = [UIImage] ()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        imgUp.append(UIImage(named: "arrow.up.png")!)
        imgDown.append(UIImage(named: "arrow.down.png")!)
        imgRight.append(UIImage(named: "arrow.right.png")!)
        imgLeft.append(UIImage(named: "arrow.left.png")!)
        
        imgUp.append(UIImage(named: "arrow.up.black.png")!)
        imgDown.append(UIImage(named: "arrow.down.black.png")!)
        imgRight.append(UIImage(named: "arrow.right.black.png")!)
        imgLeft.append(UIImage(named: "arrow.left.black.png")!)
        
        imgUp.append(UIImage(named: "arrow.up.green.png")!)
        imgDown.append(UIImage(named: "arrow.down.green.png")!)
        imgRight.append(UIImage(named: "arrow.right.green.png")!)
        imgLeft.append(UIImage(named: "arrow.left.green.png")!)
        
        imgViewUp.image = imgUp[0]
        imgViewDown.image = imgDown[0]
        imgViewRight.image = imgRight[0]
        imgViewLeft.image = imgLeft[0]
        
        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeUp.direction = UISwipeGestureRecognizer.Direction.up
        self.view.addGestureRecognizer(swipeUp)
        
        let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeDown.direction = UISwipeGestureRecognizer.Direction.down
        self.view.addGestureRecognizer(swipeDown)
        
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeRight.direction = UISwipeGestureRecognizer.Direction.right
        self.view.addGestureRecognizer(swipeRight)
        
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
        self.view.addGestureRecognizer(swipeLeft)
        
        
        
        let swipeUpMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuestureMulti(_:)))
        swipeUpMulti.direction = UISwipeGestureRecognizer.Direction.up
        swipeUpMulti.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeUpMulti)
        
        let swipeDownMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuestureMulti(_:)))
        swipeDownMulti.direction = UISwipeGestureRecognizer.Direction.down
        swipeDownMulti.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeDownMulti)
        
        let swipeRightMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuestureMulti(_:)))
        swipeRightMulti.direction = UISwipeGestureRecognizer.Direction.right
        swipeRightMulti.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeRightMulti)
        
        let swipeLeftMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuestureMulti(_:)))
        swipeLeftMulti.direction = UISwipeGestureRecognizer.Direction.left
        swipeLeftMulti.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeLeftMulti)
    }
    
    @objc func respondToSwipeGuesture(_ gesture : UIGestureRecognizer) {
        if let swipeGuesture = gesture as? UISwipeGestureRecognizer {
            imgViewUp.image = imgUp[0]
            imgViewDown.image = imgDown[0]
            imgViewRight.image = imgRight[0]
            imgViewLeft.image = imgLeft[0]
            
            switch swipeGuesture.direction {
            case UISwipeGestureRecognizer.Direction.up :
                imgViewUp.image = imgUp[1]
            case UISwipeGestureRecognizer.Direction.down :
                imgViewDown.image = imgDown[1]
            case UISwipeGestureRecognizer.Direction.right :
                imgViewRight.image = imgRight[1]
            case UISwipeGestureRecognizer.Direction.left :
                imgViewLeft.image = imgLeft[1]
            default :
                break
            }
            
            
        }
    }
    
    @objc func respondToSwipeGuestureMulti(_ gesture : UIGestureRecognizer) {
        if let swipeGuesture = gesture as? UISwipeGestureRecognizer {
            imgViewUp.image = imgUp[0]
            imgViewDown.image = imgDown[0]
            imgViewRight.image = imgRight[0]
            imgViewLeft.image = imgLeft[0]
            
            switch swipeGuesture.direction {
            case UISwipeGestureRecognizer.Direction.up :
                imgViewUp.image = imgUp[2]
            case UISwipeGestureRecognizer.Direction.down :
                imgViewDown.image = imgDown[2]
            case UISwipeGestureRecognizer.Direction.right :
                imgViewRight.image = imgRight[2]
            case UISwipeGestureRecognizer.Direction.left :
                imgViewLeft.image = imgLeft[2]
            default :
                break
            }
            
            
        }
    }


}
Posted by 목표를 가지고 달린다
,

탭-터치 이벤트에 대응하는 카운트 예제 코드

 

아래와 같이 3개의 함수를 재정의 하면 됩니다. 탭과 터치의 차이는 연속해서 클릭(터치)하냐?의 차이입니다.

  1. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)  
  2. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) 
  3. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)  

전체소스는 아래와 같습니다.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var lblMessage: UILabel!
    @IBOutlet weak var lblTapCount: UILabel!
    @IBOutlet weak var lblTouchCount: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first! as UITouch
        lblMessage.text = "Touch Began"
        lblTapCount.text = String(touch.tapCount)
        lblTouchCount.text = String(touches.count)
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first! as UITouch
        lblMessage.text = "Touch Moved"
        lblTapCount.text = String(touch.tapCount)
        lblTouchCount.text = String(touches.count)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first! as UITouch
        lblMessage.text = "Touch Ended"
        lblTapCount.text = String(touch.tapCount)
        lblTouchCount.text = String(touches.count)
    }


}
Posted by 목표를 가지고 달린다
,

동영상 플레이를 만드는 순서는 아래와 같습니다. 단순하기 때문에 쉽게 작성할 수있습니다. 
다만, 유투브 영상을 플레이하는 것이라면 어렵습니다. 
왜냐면, 유투브 동영상의 주소는 동영상(*.mp4)을 포함하는 html 문서이기 때문입니다. 

그래서 유투브 영상을 플레이하고 싶으시면, mp4 등으로 다운받아 다른 서버에 올린 후, 해당 url 을 확인하셔서 이용해야 합니다. 

<< 코딩 순서 >>

  1. import AVKit 을 포함합니다.
  2. url 을 선언합니다. (내부와 외부에 따라 다름)
  3. AVPlayer(url : url as URL)을 player로 선언합니다.
  4. AVPlayerController.player 에 위의 player를 대입합니다.
  5. player.play() 합니다.

전체소스는 아래와 같습니다.

import UIKit
import AVKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


    @IBAction func btnInternalMediaPlay(_ sender: UIButton) {
        let filePath:String? = Bundle.main.path(forResource: "mov", ofType: "MOV")
        let url = NSURL(fileURLWithPath: filePath!)
        
        playVideo(url: url)
    }
    @IBAction func btnExternalMediaPlay(_ sender: UIButton) {
        
        let url = NSURL(string: "https://dl.dropboxusercontent.com/s/e38auz050w2mvud/Fireworks.mp4")!
        
        playVideo(url: url)
    }
    
    func playVideo(url : NSURL) {
        let playerController = AVPlayerViewController()
        
        let player = AVPlayer(url: url as URL)
        playerController.player = player
        
        self.present(playerController, animated: true) {
            player.play()
        }
    }
}

Posted by 목표를 가지고 달린다
,

Swift에서 오디오를 호출하는 예제 소스

Swift 오디오 예제 화면

1. import AVFoundation 관련 라이브러리 참조

2. 변수 선언

@IBOutlet weak var pvProgressPlay: UIProgressView!
    
    var audioPlayer : AVAudioPlayer!
    var audioFile : URL!
    let MAX_VOLUME : Float = 10.0
    var progressTimer : Timer!
    
    // 플레이 타임을 프로그레이스바로 표현
    let timePlayerSelector : Selector = #selector(ViewController.updatePlaytime)

3. 파일 위치 설정하고, play 설정값을 세팅 후, audioPlayer.play() 호출

4. Record가 되지 않으면, adioRecord.record()의 반환값이 Bool 이므로, 반환값을 출력해 보면 됨.

전체 소스는 아래와 같습니다.(Record 포함)

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAudioPlayerDelegate, AVAudioRecorderDelegate {

    @IBOutlet weak var pvProgressPlay: UIProgressView!
    
    @IBOutlet weak var lblCurrentTime: UILabel!
    @IBOutlet weak var lblEndTime: UILabel!
    
    @IBOutlet weak var btnPlay: UIButton!
    @IBOutlet weak var btnPause: UIButton!
    @IBOutlet weak var btnStop: UIButton!
    
    @IBOutlet weak var slVolume: UISlider!
    
    @IBOutlet weak var swMode: UISwitch!
    @IBOutlet weak var lblMode: UILabel!
    
    @IBOutlet weak var btnRecord: UIButton!
    @IBOutlet weak var lblRecordTime: UILabel!
    
    var audioPlayer : AVAudioPlayer!
    var audioFile : URL!
    let MAX_VOLUME : Float = 10.0
    var progressTimer : Timer!
    
    let timePlayerSelector : Selector = #selector(ViewController.updatePlaytime)
    let timeRecordSelector : Selector = #selector(ViewController.updateRecordtime)
    
    var audioRecorder : AVAudioRecorder!
    var isRecordMode = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
        selectAudioFile()
        
        if !isRecordMode {
            initPlay()
            btnRecord.isEnabled = false
            lblRecordTime.isEnabled = false
        }else {
            initRecord()
        }
        
        // Do any additional setup after loading the view.
    }
    
    func selectAudioFile() {
        if !isRecordMode {
            audioFile = Bundle.main.url(forResource: "Helium", withExtension: "mp3")
            initPlay()
        } else {
            let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
            audioFile = documentDirectory.appendingPathComponent("recordFile.m4a")
        }
    }
    
    func initRecord() {
        let recordSettings = [
            AVFormatIDKey : NSNumber(value: kAudioFormatAppleLossless as UInt32),
            AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,
            AVEncoderBitRateKey : 320000,
            AVNumberOfChannelsKey : 2,
            AVSampleRateKey : 44100.0 ] as [String : Any]
        do {
            audioRecorder = try AVAudioRecorder(url: audioFile, settings: recordSettings)
        } catch let error as NSError {
            print("************ ERROR-initRecord : \(error)")
        }
        audioRecorder.delegate = self
        
        audioRecorder.isMeteringEnabled = true
        audioRecorder.prepareToRecord()
        
        slVolume.value = 1.0
        audioPlayer.volume = slVolume.value

        lblEndTime.text = convertNSTimeInterval2String(0)
        lblCurrentTime.text = convertNSTimeInterval2String(0)
        setPlayButtons(false, false, false)
        
        let session = AVAudioSession.sharedInstance()
        
        do {
            try session.setCategory(AVAudioSession.Category.playAndRecord)
        } catch let error as NSError {
            print("************ ERROR-setCategory : \(error)")
        }
        
        do {
            try session.setActive(true)
        } catch let error as NSError {
            print("ERROR-setActive : \(error)")
        }
        
    }
    
    func initPlay() {
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: audioFile)
        } catch let error as NSError {
            print("Error-initPlay : \(error)")
        }
        slVolume.maximumValue = MAX_VOLUME
        slVolume.value = 1.0
        pvProgressPlay.progress = 0
        
        audioPlayer.delegate = self
        audioPlayer.prepareToPlay()
        audioPlayer.volume = slVolume.value
        
        lblEndTime.text = convertNSTimeInterval2String(audioPlayer.duration)
        lblCurrentTime.text = convertNSTimeInterval2String(0)
        
        setPlayButtons(true, false, false)
        
    }
    
    func convertNSTimeInterval2String(_ time:TimeInterval) -> String{
        let min = Int(time/60)
        let sec = Int(time.truncatingRemainder(dividingBy: 60))
        let strTime  = String(format: "%02d:%02d", min, sec)
        return strTime
    }
    
    func setPlayButtons(_ play:Bool , _ pause:Bool, _ stop:Bool) {
        btnPlay.isEnabled = play
        btnPause.isEnabled = pause
        btnStop.isEnabled = stop
    }
    
    @IBAction func btnPlayAudio(_ sender: UIButton) {
        setPlayButtons(false, true, true)
        audioPlayer.play()
        
        progressTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: timePlayerSelector, userInfo: nil, repeats: true)
        
    }
    
    override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
    }
    
    @objc func updatePlaytime() {
        lblCurrentTime.text = convertNSTimeInterval2String(audioPlayer.currentTime)
        pvProgressPlay.progress = Float(audioPlayer.currentTime / audioPlayer.duration)
        print(audioPlayer.currentTime.description + ", " + audioPlayer.duration.description)
    }
    
    
    @IBAction func btnPauseAudio(_ sender: UIButton) {
        setPlayButtons(true, false, true)
        audioPlayer.pause()
    }
    
    @IBAction func btnStopAudio(_ sender: UIButton) {
        setPlayButtons(true, false, false)
        pvProgressPlay.progress = 0
        audioPlayer.stop()
        audioPlayer.currentTime = 0
        lblCurrentTime.text = convertNSTimeInterval2String(0)
        progressTimer.invalidate()
    }
    
    
    @IBAction func slChangeVolume(_ sender: UISlider) {
        audioPlayer.volume = slVolume.value
    }
    
    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        progressTimer.invalidate()
        setPlayButtons(true, false, false)
        
    }
    
    @IBAction func swChangeMode(_ sender: UISwitch) {
        if sender.isOn{
            audioPlayer.stop()
            audioPlayer.currentTime = 0
            lblRecordTime!.text = convertNSTimeInterval2String(0)
            isRecordMode = true
            btnRecord.isEnabled = true
            lblRecordTime.isEnabled = true
        }else {
            isRecordMode = false
            btnRecord.isEnabled = false
            lblRecordTime.isEnabled = false
            lblRecordTime.text = convertNSTimeInterval2String(0)
        }
        
        selectAudioFile()
        
        if !isRecordMode {
            initPlay()
        } else {
            initRecord()
        }
    }
   
    @IBAction func btnRecord(_ sender: UIButton) {
        //if ( sender as AnyObject).titleLabel?.text == "Record" {
        if sender.titleLabel?.text == "Record" {
            var isRecording : Bool = audioRecorder.record()
            print("IS RECORDING? : ", isRecording.description)
            //(sender as AnyObject).setTitle("Stop", for: UIControl.State())
            sender.setTitle("Stop", for: UIControl.State())
            progressTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: timeRecordSelector, userInfo: nil, repeats: true)
        } else {
            audioRecorder.stop()
            progressTimer.invalidate()
            (sender as AnyObject).setTitle("Record", for: UIControl.State())
            btnPlay.isEnabled = true
            initPlay()
        }
    }
    
    @objc func updateRecordtime() {
        lblRecordTime.text = convertNSTimeInterval2String(audioRecorder.currentTime)
    }
    
    
}
Posted by 목표를 가지고 달린다
,

this class is not key value coding-compliant for the key btnSize

책 예제를 따라하거나, 이미 만들어진 ViewController와 mainstory.swift를 합칠 경우,

내부 Widget에 정의한 action에 대해 연결이 깨진 경우가 발생한다.

확인하려면, Xcode에서 Triggered Segues에 링크가 깨져서 노란색으로 경고를 띄워주는 것을 발견할 수 있다. 이것을 다시 만들어서 정의해 주면 된다. 

Swift 오류 확인하는 화면

오류 내용은 아래와 같이 나타난다. 

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x103a17520> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key btnSize.'
*** First throw call stack:
(
	0   CoreFoundation                      0x00000001804b910c __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x0000000180092da8 objc_exception_throw + 72
	2   CoreFoundation                      0x00000001804b8ca0 -[NSException init] + 0
	3   Foundation                          0x0000000180e9da3c -[NSObject(NSKeyValueCoding) setValue:forKey:] + 268
	4   UIKitCore                           0x00000001853b7dc4 -[UIViewController setValue:forKey:] + 76
	5   UIKitCore                           0x00000001857435ec -[UIRuntimeOutletConnection connect] + 80
	6   CoreFoundation                      0x00000001804a5cf0 -[NSArray makeObjectsPerformSelector:] + 192
	7   UIKitCore                           0x0000000185736ee4 -[UINib instantiateWithOwner:options:] + 1420
    8   UIKitCore                           0x00000001853bf7a8 -[UIViewController loadView] + 392
	9   UIKitCore                           0x00000001853bfa64 -[UIViewController loadViewIfRequired] + 152
	10  UIKitCore                           0x00000001853c0000 -[UIViewController view] + 20
	11  UIKitCore                           0x00000001852f4628 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 912
	12  UIKitCore                           0x00000001852eef70 -[UITabBarController _setSelectedViewController:performUpdates:] + 352
	13  UIKitCore                           0x00000001852eedd4 -[UITabBarController setSelectedViewController:] + 80
	14  UIKitCore                           0x00000001852f3a98 -[UITabBarController _setSelectedViewControllerAndNotify:] + 216
	15  UIKitCore                           0x00000001852f3964 -[UITabBarController _tabBarItemClicked:] + 160
	16  UIKitCore                           0x0000000185b36e3c -[UIApplication sendAction:to:from:forEvent:] + 96
	17  UIKitCore                           0x00000001850f969c -[UITabBar _sendAction:withEvent:] + 380
	18  UIKitCore                           0x0000000185b36e3c -[UIApplication sendAction:to:from:forEvent:] + 96
	19  UIKitCore                           0x000000018540c830 -[UIControl sendAction:to:forEvent:] + 108
	20  UIKitCore                           0x000000018540cb74 -[UIControl _sendActionsForEvents:withEvent:] + 268
	21  UIKitCore                           0x00000001850fbfb0 -[UITabBar _buttonUp:] + 96
	22  UIKitCore                           0x0000000185b36e3c -[UIApplication sendAction:to:from:forEvent:] + 96
	23  UIKitCore                           0x000000018540c830 -[UIControl sendAction:to:forEvent:] + 108
	24  UIKitCore                           0x000000018540cb74 -[UIControl _sendActionsForEvents:withEvent:] + 268
	25  UIKitCore                           0x000000018540b80c -[UIControl touchesEnded:withEvent:] + 392
	26  UIKitCore                           0x0000000185b6aa10 -[UIWindow _sendTouchesForEvent:] + 972
	27  UIKitCore                           0x0000000185b6be20 -[UIWindow sendEvent:] + 2840
	28  UIKitCore                           0x0000000185b4b80c -[UIApplication sendEvent:] + 376
	29  UIKitCore                           0x0000000185bd5c70 __dispatchPreprocessedEventFromEventQueue + 1156
	30  UIKitCore                           0x0000000185bd8c00 __processEventQueue + 5592
	31  UIKitCore                           0x0000000185bd0f10 updateCycleEntry + 156
	32  UIKitCore                           0x00000001850a5cec _UIUpdateSequenceRun + 76
	33  UIKitCore                           0x0000000185a60858 schedulerStepScheduledMainSection + 168
	34  UIKitCore                           0x0000000185a5fc90 runloopSourceCallback + 80
	35  CoreFoundation                      0x000000018041d294 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
	36  CoreFoundation                      0x000000018041d1dc __CFRunLoopDoSource0 + 172
	37  CoreFoundation                      0x000000018041c940 __CFRunLoopDoSources0 + 232
	38  CoreFoundation                      0x0000000180416e84 __CFRunLoopRun + 788
	39  CoreFoundation                      0x00000001804166f4 CFRunLoopRunSpecific + 552
	40  GraphicsServices                    0x00000001905e5b10 GSEventRunModal + 160
	41  UIKitCore                           0x0000000185b319dc -[UIApplication _run] + 796
	42  UIKitCore                           0x0000000185b35bd4 UIApplicationMain + 124
	43  UIKitCore                           0x0000000184f0a334 block_destroy_helper.22 + 9660
	44  Tab.debug.dylib                     0x0000000102f0103c $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 120
	45  Tab.debug.dylib                     0x0000000102f00fb4 $s3Tab11AppDelegateC5$mainyyFZ + 44
	46  Tab.debug.dylib                     0x0000000102f010b8 __debug_main_executable_dylib_entry_point + 28
	47  dyld                                0x0000000102479410 start_sim + 20
	48  ???                                 0x00000001025ca154 0x0 + 4334592340
	49  ???                                 0x9874000000000000 0x0 + 10985405391063482368
)
libc++abi: terminating due to uncaught exception of type NSException
Exception	NSException *	
"[<UIViewController 0x103a17520> setValue:forUndefinedKey:]: 
this class is not key value coding-compliant for the key btnSize."	0x0000600000c9dfe0

 

Posted by 목표를 가지고 달린다
,

익명함수(클로져, Closure) 이해하기

  함수 비고
함수원본 func 함수명 ( 파라미터명 : 자료형) -> (반환자료형) {
             실행 구문
}
보통함수
1 { ( 파라미터명 : 자료형) -> (반환타입) in 실행 구문 } 익명함수
2 { (파라미터명) in 실행 구문 }
3 { 파라미터명 in 실행 구문 }

위의 방식을 이용한 아래의 예제를 보시면서 이해해보세요.

  함수 비고
함수원본 func completeWork(completed : Bool) -> () {
    print ("complete : \(completed)") 
보통함수
1 { (completed: Bool) -> () in print ("complete : \(completed)")  } 익명함수

2 { (completed: Bool)         in print ("complete : \(completed)")  }
3 { (completed)                  in print ("complete : \(completed)")  }
4 { completed                    in print ("complete : \(completed)")  }

위와 같이 실행구문은 변함이 없으나, 함수 호출을 정의하는 부분이 많이 축약(함수명, 파라미터의 자료형 등) 됩니다.

사실 클로져에 대한 활용은 많은 예시문을 별도로 보관하고 계시다가 발췌해서 이용하거나, 기존 것을 활용해서 수정해서 사용하시면서 익히시는 것이 많은 도움이 될 것입니다. 특히, 집합 관련하여 filter 등을 활용한 예제들은 소스코드를 많이 간결하게 만들어주기 때문에 꼭 한번 찾아 보세요.

Posted by 목표를 가지고 달린다
,