您好,登錄后才能下訂單哦!
在Swift中,可以使用UIImagePickerController類來調(diào)用相機(jī)和相冊。以下是一個簡單的示例代碼,演示了如何調(diào)用相機(jī)和相冊:
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)
}
}
在這個示例中,我們創(chuàng)建了一個ViewController類,并實(shí)現(xiàn)了UIImagePickerControllerDelegate和UINavigationControllerDelegate協(xié)議。我們在takePhoto和choosePhoto方法中分別調(diào)用相機(jī)和相冊。在imagePickerController和imagePickerControllerDidCancel方法中,我們處理選取照片和取消選取照片的邏輯。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。