您好,登錄后才能下訂單哦!
在Swift中,可以使用UIImagePickerController類來調用相機和相冊。以下是一個簡單的示例代碼,演示了如何調用相機和相冊:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func takePhoto(_ sender: UIButton) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
@IBAction func choosePhoto(_ sender: UIButton) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
// 在此處處理選擇的照片
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
在這個示例中,我們創建了一個ViewController類,并實現了UIImagePickerControllerDelegate和UINavigationControllerDelegate協議。我們在takePhoto和choosePhoto方法中分別調用相機和相冊。在imagePickerController和imagePickerControllerDidCancel方法中,我們處理選取照片和取消選取照片的邏輯。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。