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)
}
}
}
320x100
'프로그래밍 > iOS-Swift' 카테고리의 다른 글
[SWIFT] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set? (0) | 2021.04.06 |
---|---|
[SWIFT] DatePicker 생년월일 Wheel Style (0) | 2021.04.05 |
맥북으로 아이패드 혹은 아이폰 UUID 확인하기 (0) | 2021.04.05 |
[SWIFT] Tap Gesture - Keyboard Hide (0) | 2021.04.05 |
[SWIFT] EXTENSION (0) | 2021.04.05 |
댓글