iT邦幫忙

2021 iThome 鐵人賽

DAY 27
0
Mobile Development

ios 的小小實驗室 !系列 第 27

DAY 27『 使用相機拍照 』 ImagePicker - Part1

今天分享 ImagePicker 如何使用相機拍照


拉一個 Button 、 ImageView

在 Info.plist 添加一個 key,描述訪問權限。

  1. 打開 Info.plist。

  2. 右鍵單擊信息屬性列表並選擇添加行。出現一個新行。

  3. 輸入 Key:
    Main nib file base name // 指定應用程式主 nib 文件的名稱
    Privacy - Camera Usage Description // 使用相機的權限

  4. 在 Privacy - Camera Usage Description 的 Value 中,輸入使用相機理由。當 iOS 首次請求訪問庫的權限時,它會顯示此信息。

在 @IBAction 裡
寫一個 Alert 偵測是否有相機功能
沒有相機 -> print("ERROR:沒有相機功能!")

if !UIImagePickerController.isSourceTypeAvailable(.camera) {
    let alertController = UIAlertController(title: "ERROR", message: "Device has no camera.", preferredStyle: .alert)
    let okAction = UIAlertAction(title: "Alright", style: .default, handler: { (alert: UIAlertAction!) in
    })
    alertController.addAction(okAction)
    self.present(alertController, animated: true, completion: nil)
    print("ERROR:沒有相機功能!")
} else { // 若有相機功能,將執行的事 }

明天會分享如何用 extension 去寫 UIImagePickerControllerDelegate 的委任,敬請期待!


上一篇
DAY 26 『 AVPlayerViewController - 播放影片 』
下一篇
DAY 28『 使用相機拍照 』 ImagePicker - Part2
系列文
ios 的小小實驗室 !30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言