본문 바로가기
프로그래밍/iOS-Swift

[SWIFT] Camera & Gallery Image Pick

by 채연2 2021. 4. 5.

 

 

 

 

 

1. 버튼 추가하기

class ViewController3 : UIViewController {
    @IBOutlet weak var cameraBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        cameraBtn.setImage(UIImage(named: "cameraicon.png"), for: .normal)
    }
}

 

 

 

2. 버튼 이벤트 추가하기

    @IBAction func imgPickerBtn(_ sender: UIButton) {
        let alert = UIAlertController(title: "Select one.", message: nil, preferredStyle: .actionSheet)
        let library = UIAlertAction(title: "사진앨범", style: .default) { (action) in
            self.openLibrary()
        }
        let camera = UIAlertAction(title: "카메라", style: .default) { (action) in
            self.openCamera()
        }
        let cancel = UIAlertAction(title: "취소", style: .cancel, handler: nil)
        
        alert.addAction(library)
        alert.addAction(camera)
        alert.addAction(cancel)
        
        present(alert, animated: true, completion: nil)
    }

- Action Sheet를 클릭하면 handler에 정의해준 함수 실행됨

 

 

 

3. info.plist에 접근권한 설정하기

- test 대신에 설명을 써주면 된다.

 

 

 

320x100

 

 

4. delegate 채택과 UIImagePickerController instance 생성하기

class ViewController3 : UIViewController {
    let picker = UIImagePickerController()
    ...
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        cameraBtn.setImage(UIImage(named: "cameraicon.png"), for: .normal)
        picker.delegate = self
    }
    
    ...
}

extension ViewController3 : UIImagePickerControllerDelegate & UINavigationControllerDelegate {
    func openLibrary() {
        picker.sourceType = .photoLibrary
        present(picker, animated: false, completion: nil)
    }
    
    func openCamera() {
        picker.sourceType = .camera
        present(picker, animated: false, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            pickImgview.image = image
            dismiss(animated: true, completion: nil)
        }
    }
}

 

 

 

5. ImageView 추가 및 이벤트 처리하기

class ViewController3 : UIViewController {
    ...
    @IBOutlet weak var pickImgview: UIImageView!
    
    ...
}

extension ViewController3 : UIImagePickerControllerDelegate & UINavigationControllerDelegate {
    ...
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            pickImgview.image = image
            dismiss(animated: true, completion: nil)
        }
    }
}

- didFinishPickingMediaWithInfo : 사진을 pick한 후 처리할 내용 이 메소드 안에 정의해주면 됨.

- dismiss를 해주지 않으면 pick한 후 imagePickerController가 사라지지 않고 계속 머물러 있음...

 

 

6. 결과

 

 

 

 

 

 

7. 최종 코드


import Foundation
import UIKit

class ViewController3 : UIViewController {
    let picker = UIImagePickerController()
    @IBOutlet weak var cameraBtn: UIButton!
    @IBOutlet weak var pickImgview: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        cameraBtn.setImage(UIImage(named: "cameraicon.png"), for: .normal)
        picker.delegate = self
    }
    
    @IBAction func imgPickerBtn(_ sender: UIButton) {
        let alert = UIAlertController(title: "Select one.", message: nil, preferredStyle: .actionSheet)
        let library = UIAlertAction(title: "사진앨범", style: .default) { (action) in
            self.openLibrary()
        }
        let camera = UIAlertAction(title: "카메라", style: .default) { (action) in
            self.openCamera()
        }
        let cancel = UIAlertAction(title: "취소", style: .cancel, handler: nil)
        
        alert.addAction(library)
        alert.addAction(camera)
        alert.addAction(cancel)
        
        present(alert, animated: true, completion: nil)
    }
}

extension ViewController3 : UIImagePickerControllerDelegate & UINavigationControllerDelegate {
    func openLibrary() {
        picker.sourceType = .photoLibrary
        present(picker, animated: false, completion: nil)
    }
    
    func openCamera() {
        picker.sourceType = .camera
        present(picker, animated: false, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            pickImgview.image = image
            dismiss(animated: true, completion: nil)
        }
    }
}

 

 

 

참고 : zeddios.tistory.com/125

 

iOS ) 내 사진앨범/카메라에서 이미지 가져오기

안녕하세요 :) Zedd입니다. 오늘 해볼거는 내 사진앨범에 있는 이미지를 가져오거나, 카메라로 찍은 이미지를 가져올거에요. 너무 잼나겠다 그쵸 ㅎㅎ 오늘 우리가 만들어볼거는 바로 요런거 입

zeddios.tistory.com

 

 

 

 

 

 

320x100

댓글