見出し画像

Swift 勉強14.カメラとフォトライブラリからメディアをインポート

1.NEW Project

2.👇画像のようにUIImageView/Buttonを配置する

3.アウトレット変数とアクション関数の追加

    @IBAction func btnCaptureImageFromCamera(_ sender: UIButton) {
   }
   @IBAction func btnLoadImageFromLibrary(_ sender: UIButton) {
   }
   @IBAction func btnRecordVideoFromCamera(_ sender: UIButton) {
   }
   @IBAction func btnLoadViedoFromLibrary(_ sender: UIButton) {
   }

4.各ボタンに必要な機能を実装する

ViewController.swift

👉「警告表示用」のメソッドを作成
    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)
   }
👉「写真撮影」のコードを作成
    @IBAction func btnCaptureImageFromCamera(_ sender: UIButton) {
       if (UIImagePickerController.isSourceTypeAvailable(.camera)) {
           flagImageSave = true

//カメラの使用可能かどうかを確認/カメラ撮影後救うので、画像の保存を許可するか
   
//画像ピッカーのデリゲートをselfに設定
//画像ピッカーのソースタイプをcameraに設定
//メディアタイプの設定
//編集は許可されない
       
           imagePicker.delegate = self
           imagePicker.sourceType = .camera
           imagePicker.mediaTypes = [kUTTypeImage as String]
           imagePicker.allowsEditing = false
           
           present(imagePicker, animated: true, completion: nil)
       }

//現在のビューコントローラをimagePickerに置き換え

       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 = [kUTTypeMovie as String]
           imagePicker.allowsEditing = flagImageSave
           
           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 = false


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

  }
  else { myAlert(“Photo album inaccessable”, message: “Application cannot
                  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)
   }

\終わり/ 写真とビデオ撮影可能・読むこみも可能


\コラージュも作ることができる/