swift 实现拍照 选择相册

//点击按钮的方法

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

/// 屏幕底部弹出的Alert

 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 }
 //拍照与本地相册方法/ 

// 去拍照

 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 }

//代理

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
 

猜你喜欢

转载自www.cnblogs.com/-yun/p/10654209.html