swift 實現拍照 選擇相冊

//點擊按鈕的方法ios

1 func photos()  {
2     
3   self.showBottomAlert()
4     
5 }

 

/// 屏幕底部彈出的Alertui

 1 func showBottomAlert(){
 2     
 3     let alertController=UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
 4 
 5     let cancel=UIAlertAction(title:"取消", style: .cancel, handler: nil)
 6     let takingPictures=UIAlertAction(title:"拍照", style: .default)
 7     {
 8         action in
 9         self.goCamera()
10         
11     }
12     let localPhoto=UIAlertAction(title:"本地圖片", style: .default)
13     {
14         action in
15         self.goImage()
16         
17     }
18     alertController.addAction(cancel)
19     alertController.addAction(takingPictures)
20     alertController.addAction(localPhoto)
21     self.present(alertController, animated:true, completion:nil)
22     
23 }

 

 //拍照與本地相冊方法/ 

// 去拍照spa

 1 func goCamera(){
 2                 
 3     if UIImagePickerController.isSourceTypeAvailable(.camera){
 4         let  cameraPicker = UIImagePickerController()
 5         cameraPicker.delegate = self
 6         cameraPicker.allowsEditing = true
 7         cameraPicker.sourceType = .camera
 8         //在須要的地方present出來
 9         self.present(cameraPicker, animated: true, completion: nil)
10     } else {
11         
12         print("不支持拍照")
13         
14     }
15 
16 }

 

/// 去相冊代理

 1 func goImage(){
 2 
 3     
 4     let photoPicker =  UIImagePickerController()
 5     photoPicker.delegate = self
 6     photoPicker.allowsEditing = true
 7     photoPicker.sourceType = .photoLibrary
 8     //在須要的地方present出來
 9     self.present(photoPicker, animated: true, completion: nil)
10     
11 }

 

//代理code

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {


    print("得到照片============= \(info)")
    
    let image : UIImage = info[UIImagePickerController.InfoKey.editedImage] as! UIImage

    //顯示設置的照片
    imgView.image = image

    self.dismiss(animated: true, completion: nil)
}
做者:江河_ios 連接:https://www.jianshu.com/p/fc0768999ef8
相關文章
相關標籤/搜索