iOS 中的照相和照片功能如何實現呢?,其實非常容易,下面介紹如何使用 Camera 與 album 的存取:
首先在 info.plist 中打開以下兩個權限:
Privacy — Camera Usage Description:請求使用 Camera。
Privacy — Photo Library Usage Description:請求讀取 photo album
func ShowAlbum() {
// * 然後再下面設定相簿的來源和 delegate
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
present(imagePicker, animated: true, completion: nil)
}
extension TripInfoViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// * 繼承 UIImagePickerControllerDelegate、UINavigationControllerDelegate
* 重點是 info[.originalImage]
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.originalImage] as? UIImage {
coverImage.image = image
self.dismiss(animated: false, completion: nil)
}
}
}