먼저 영상 촬영과 저장을 위해 권한 설정을 해야한다.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func videoButton(_ sender: UIButton) {
let camera = UIImagePickerController()
camera.delegate = self
camera.sourceType = .camera
camera.mediaTypes = UIImagePickerController.availableMediaTypes(for: .camera) ?? []
self.present(camera, animated: true, completion: nil)
}
}
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//동영상 저장
if let url = info[.mediaURL] as? URL, UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(url.path) {
UISaveVideoAtPathToSavedPhotosAlbum(url.path, self, #selector(savedVideo), nil)
}
picker.dismiss(animated: true, completion: nil)
}
@objc func savedVideo(_ videoPath: String, didFinishSavingWithError error: Error?, contextInfo: UnsafeMutableRawPointer?) {
if let error = error {
print(error)
return
}
print("success")
}
}
'iOS > Swift' 카테고리의 다른 글
[Swift] UI 구현 시 Storyboard와 Code Base 비교 (0) | 2022.10.26 |
---|---|
[iOS/Swift] Alamofire로 서버(FastAPI)에 동영상 업로드하기 (0) | 2022.05.23 |
[iOS] Charts 라이브러리 설치방법 및 사용방법 (0) | 2022.03.03 |
[iOS] Swift 라이브러리 typing animation (0) | 2021.08.23 |
[iOS/Swift] Xcode Simulator 키보드 안보일때 (0) | 2021.05.08 |