📷Swift UIImagePickerController | 選擇照片 & 拍照 📷
在 Swift 中,我們可以使用 UIImagePickerController 來選擇照片或拍照,並將照片存入相簿中。在本文中,我們將介紹如何使用 UIImagePickerController 來選擇照片或拍照,並將照片存入相簿中。
前置準備
在開始之前,我們需要先在 info.plist 中加入以下兩個鍵值:
- NSPhotoLibraryUsageDescription:用於存取相簿的描述
- NSCameraUsageDescription:用於存取相機的描述
接著,我們需要在 ViewController.swift 中加入以下程式碼:
import UIKit
import Photos
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// ...
}
選擇照片
首先,我們需要先檢查使用者是否允許存取相簿:
func checkPhotoLibraryPermission() {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
// 已授權存取相簿
break
case .denied, .restricted :
// 拒絕存取相簿
break
case .notDetermined:
// 尚未決定是否授權存取相簿
PHPhotoLibrary.requestAuthorization { status in
switch status {
case .authorized:
// 已授權存取相簿
break
case .denied, .restricted:
// 拒絕存取相簿
break
case .notDetermined:
// 尚未決定是否授權存取相簿
break
}
}
break
}
}
接著,我們可以使用 UIImagePickerController 來選擇照片:
func selectPhoto() {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}
}
最後,我們需要實作 UIImagePickerControllerDelegate 中的 didFinishPickingMediaWithInfo 方法,以取得使用者選擇的照片:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
// 取得使用者選擇的照片
}
picker.dismiss(animated: true, completion: nil)
}
拍照
首先,我們需要先檢查使用者是否允許存取相機:
func checkCameraPermission() {
let status = AVCaptureDevice.authorizationStatus(for: .video)
switch status {
case .authorized:
// 已授權存取相機
break
case .denied, .restricted :
// 拒絕存取相機
break
case .notDetermined:
// 尚未決定是否授權存取相機
AVCaptureDevice.requestAccess(for: .video) { granted in
if granted {
// 已授權存取相機
} else {
// 拒絕存取相機
}
}
break
}
}
接著,我們可以使用 UIImagePickerController 來拍照:
func takePhoto() {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}
}
最後,我們需要實作 UIImagePickerControllerDelegate 中的 didFinishPickingMediaWithInfo 方法,以取得使用者拍攝的照片:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
// 取得使用者拍攝的照片
}
picker.dismiss(animated: true, completion: nil)
}
將照片存入相簿
最後,我們可以使用 PHPhotoLibrary 來將照片存入相簿:
func savePhotoToAlbum(image: UIImage) {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: image)
}) { saved, error in
if saved {
// 照片已存入相簿
}
}
}
總結
在本文中,我們介紹了如何使用 UIImagePickerController 來選擇照片或拍照,並將照片存入相簿中。
推薦閱讀文章
推薦閱讀文章
1. Swift UIImagePickerController for taking photos with camera or choose from library
2. How to select a photo from the camera roll
3. UIImagePickerController in Swift
4. UIImagePickerController Tutorial for iOS
5. UIImagePickerController Tutorial for iOS</a